Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何对Rxjs CombineTest进行单元测试?_Angular_Unit Testing_Testing_Rxjs_Karma Jasmine - Fatal编程技术网

Angular 如何对Rxjs CombineTest进行单元测试?

Angular 如何对Rxjs CombineTest进行单元测试?,angular,unit-testing,testing,rxjs,karma-jasmine,Angular,Unit Testing,Testing,Rxjs,Karma Jasmine,我的组件是ngOnInit,它能做到这一点 public ngOnInit() { this.ctrSubscription = combineLatest(this.route.queryParams, this.tags$).subscribe(async params => { if (Object.keys(params[0]).length > 0) { this.initView({ ...params[0] }); }

我的组件是ngOnInit,它能做到这一点

public ngOnInit() {
    this.ctrSubscription = combineLatest(this.route.queryParams, this.tags$).subscribe(async params => {
      if (Object.keys(params[0]).length > 0) {
        this.initView({ ...params[0] });
      }
      if (Object.keys(params[1]).length > 0) {
        this.wsTags = Object.values(params[1]);
      }
      if (params[0] && this.wsTags.length > 0) {
        this.searchWs({ ...params[0] }.q);
        this.pruneData();
      }
    });
  }
这就是我测试它的方式

  it('should perform search and load the results', fakeAsync(() => {
    TestBed.get(ActivatedRoute).queryParams = of({ q: 'test' });
    const initSpy = spyOn<any>(component, 'initView').and.callThrough();
    const subSpy = spyOn<any>(component.tags$, 'subscribe');
    component.wsTags = ensureState(mockTags as any);
    flushMicroTasks();
    fixture.detectChanges();
    flushMicroTasks();
    expect(initSpy).toHaveBeenCalledWith({ q: 'test' });
    expect(subSpy).toHaveBeenCalled();
    expect(component.wsTags).toBe(Object.values(mockTags));
  }));
it('应该执行搜索并加载结果'),fakeAsync(()=>{
get(ActivatedRoute).queryParams=of({q:'test'});
const initSpy=spyOn(组件'initView')。和.callThrough();
const subpy=spyOn(component.tags$,'subscribe');
component.wsTags=EnsureRate(如有模拟标签);
刷新微任务();
fixture.detectChanges();
刷新微任务();
expect(initSpy).tohavencalledwith({q:'test'});
expect(subpy).tohavebeincall();
expect(component.wsTags).toBe(Object.values(mockTags));
}));
间谍根本没有被调用。我在运行测试时放置了一个断点,以了解值是否传递,并且我能够捕获正确传递的查询参数,但if仍然失败


我试着浏览文档,以便更好地理解这里关于堆栈溢出的各种类似问题,但我无法解决它。我对编写单元测试还比较陌生,所以如果有任何帮助,我将不胜感激。谢谢。

一个潜在的问题是,您在
it
块中模拟
ActivatedRoute
。这太晚了,您的
ngonit
将在
compileComponents
之后的第一次
detectChanges
之后运行

配置测试模块时,请尝试:

describe('Put description of the test', () => {
  let component: YourComponent;
  let activatedRoute: ActivatedRoute;
  let fixture: ComponentFixture<YourComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
     declarations: [ YourComponentToTest, 
      // other declarations if need be.
     ],
     providers: [
      provide: ActivatedRoute,
      useValue: {
       queryParams: of({ q: 'test" }),
      }
     ]
   }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(YourComponentToTest);
    component = fixture.componentInstance;
    activatedRoute = TestBed.get(ActivatedRoute);
    // ngOnInit is ran now -- after the first detectChanges
    fixture.detectChanges();
  });

  it('should do what you want', () => {
   // your arrange, act, and assertions.
  });
})
description('Put description of the test',()=>{
让组件:你的组件;
让activatedRoute:activatedRoute;
let夹具:组件夹具;
在每个之前(()=>{
TestBed.configureTestingModule({
声明:[您的组件测试,
//如有需要,可提交其他声明。
],
供应商:[
提供:激活的路由,
使用价值:{
queryParams:of({q:'test}),
}
]
}).compileComponents();
});
在每个之前(()=>{
fixture=TestBed.createComponent(您的ComponentToTest);
组件=fixture.componentInstance;
activatedRoute=TestBed.get(activatedRoute);
//ngOnInit现在运行--在第一次检测更改之后
fixture.detectChanges();
});
它('应该做你想做的',()=>{
//你的安排、行动和主张。
});
})
确保
this.tags$
也已初始化

所有这些我都是徒手完成的,所以可能会有打字错误