Angular RxJs:测试依赖于其他可观察对象的可观察对象/

Angular RxJs:测试依赖于其他可观察对象的可观察对象/,angular,testing,rxjs,observable,angular-spectator,Angular,Testing,Rxjs,Observable,Angular Spectator,我有一个名为isSearchEnabled$的可观察对象,它指示搜索表单上的搜索按钮是否可单击。 这是可观察的代码: isSearchEnabled$=anotherObject.errorSource$.pipe(映射((错误)=>errors.length==0); 可以看出,这个可观察对象依赖于另一个可观察对象,它在每次表单验证时都会发出一个数组。因此,当数组为空时,isSearchEnabled会发出一个布尔值true,否则为false 我想测试这个可观察对象,但我不确定如何测试。我应该

我有一个名为isSearchEnabled$的可观察对象,它指示搜索表单上的搜索按钮是否可单击。 这是可观察的代码:

isSearchEnabled$=anotherObject.errorSource$.pipe(映射((错误)=>errors.length==0);

可以看出,这个可观察对象依赖于另一个可观察对象,它在每次表单验证时都会发出一个数组。因此,当数组为空时,isSearchEnabled会发出一个布尔值true,否则为false

我想测试这个可观察对象,但我不确定如何测试。我应该模拟另一个对象。errorSource$以发出假错误,并检查
isSearchEnabled$
是否返回true或false

我应该如何在Angular/RxJS中实现它? 如果有人在《旁观者》中有解决方案,或者只是一个常规解决方案,我会非常感激


注:我知道我应该提供一个工作示例,但我认为这个描述非常简单,不需要进一步的代码。

如果另一个对象是您上面指定的对象,您可以这样做

description(“HelloComponent”,()=>{
让观众:观众;
const createComponent=createComponentFactory({
组件:HelloComponent,
提供者:[],
检测更改:错误
});
在每个之前(()=>{
旁观者=createComponent();
});
它(“应该被禁用”,完成=>{
常量错误=[新错误(“xyz”)、新错误(“abc”)];
旁观者.component.anotherObject={errorSource$:of(errors)};
观众。检测变化();
旁观者.component.isSearchEnabled$。订阅({
下一步:isSearchEnabled=>{
expect(isSearchEnabled).toBeFalse();
//您可能需要在此处测试组件模板/状态是否已更改。。
完成();
}
});
});
它(“应该启用”,完成=>{
旁观者.component.anotherObject={errorSource$:of([]);
观众。检测变化();
旁观者.component.isSearchEnabled$。订阅({
下一步:isSearchEnabled=>{
expect(isSearchEnabled).toBeTrue();
完成();
}
});
});
});
如果您从秋田查询中获得可观测值,那么最好的选择是模拟您的可观测值所依赖的查询方法。我没有测试下面的代码,但类似的方法应该可以工作

  let spectator: Spectator<HelloComponent>;
  let query: SpyObject<AkitaAnotherObjectQuery>;

  const createComponent = createComponentFactory({
    component: HelloComponent,
    mocks: [AkitaAnotherObjectQuery],
    detectChanges: false
  });

  beforeEach(() => {
    spectator = createComponent();
    query = spectator.inject(AkitaAnotherObjectQuery);
    //return empty array to check if search is enabled by default
    query.selectErrorSource.andReturn(of([]));
  });

  it('should be enabled', () => {
     spectator.detectChanges();
     //todo test whether component state changed..
  });

  //test could be fake async or if you want to test the observable you can subscribe to it in the test below
  it('should be disabled', () => {
    const errors = [new Error('myError')];
    query.selectErrorSource.andReturn(of(errors));
    spectator.detectChanges();
    //todo test whether component state changed..
   });

  ....

让观众观看:观众;
let query:SpyObject;
const createComponent=createComponentFactory({
组件:HelloComponent,
mocks:[AkitaAnotherObjectQuery],
检测更改:错误
});
在每个之前(()=>{
旁观者=createComponent();
query=旁观者.inject(AkitaAnotherObjectQuery);
//返回空数组以检查默认情况下是否启用了搜索
query.selectErrorSource.andReturn(共[]);
});
它('应该启用',()=>{
观众。检测变化();
//todo测试组件状态是否已更改。。
});
//测试可以是伪异步的,或者如果您想测试可观察的,您可以在下面的测试中订阅它
它('应该被禁用',()=>{
常量错误=[newerror('myError')];
query.selectErrorSource.andReturn(of(errors));
观众。检测变化();
//todo测试组件状态是否已更改。。
});
....

希望成功,如果您还有任何问题,请告诉我。如果您需要,我还可以在Stackblitz中添加akita查询模拟的示例,请告诉我。

另一个对象是服务,或者只是在errorSource$subject上发出错误的对象?另一个对象是akita查询(状态管理查询)这会在更改时从存储中发出项目。但我认为唯一重要的是errorSource$发出的内容,对吗?谢谢,这帮了大忙,但最终我以另一种方式做了,用主题模仿了
errorSource$
,用
next()发出了错误
,然后我检查isSearchEnabled$emits是true还是false。有没有办法使用marbles/TestScheduler来实现这一点?是的,它们允许您同步测试异步操作。在您上面的问题中,我认为您不需要它。据我所知,您希望测试用户是否可以在组件级别使用搜索输入。我如果您希望根据您的状态更改执行多个不同的查询,那么您希望在查询/服务级别而不是组件级别上深入测试操作。这同样有效,祝您好运!
  let spectator: Spectator<HelloComponent>;
  let query: SpyObject<AkitaAnotherObjectQuery>;

  const createComponent = createComponentFactory({
    component: HelloComponent,
    mocks: [AkitaAnotherObjectQuery],
    detectChanges: false
  });

  beforeEach(() => {
    spectator = createComponent();
    query = spectator.inject(AkitaAnotherObjectQuery);
    //return empty array to check if search is enabled by default
    query.selectErrorSource.andReturn(of([]));
  });

  it('should be enabled', () => {
     spectator.detectChanges();
     //todo test whether component state changed..
  });

  //test could be fake async or if you want to test the observable you can subscribe to it in the test below
  it('should be disabled', () => {
    const errors = [new Error('myError')];
    query.selectErrorSource.andReturn(of(errors));
    spectator.detectChanges();
    //todo test whether component state changed..
   });

  ....