this.variable = this.service.getMydata();
上面的服务返回Observable,并且对于我已订阅的变量如下。
this.variable.subscribe(data => { My logic here});
正确的单元将如何在角度6中对此进行测试?
尝试一下:
const mockResponse = {
// ...
};
spyOn(service, 'getMydata').and.returnValue(of(mockResponse));
模拟服务和返回可观察的方法
const mockService = {
getMydata: {
return of(<Response>)
}
}
TestBed.configureTestingModule({
declarations: [...],
providers: [{provide: Service, useValue: mockService}]
})