【发布时间】:2022-01-24 18:03:06
【问题描述】:
在我的 Angular 应用程序中,我正在尝试编写一个单元测试用例,其中我将其余服务响应模拟为 Observable。所以,我在我的测试用例中使用了 fakeAsync & tick。但还是失败了。
service.ts
import { HttpClient } from "@angular/common/http";
@Injectable()
export class RestServices{
constructor(private _http: HttpClient){}
public post(body: any): void{
// have variables initializations
this._http
.post(url, body, { observe: "response" })
.subscribe(
response => this._onSuccess(response),
error => this._onFailure(error);
);
}
private _onSuccess(response: any){
if(response.status == 200){
// extract data from response
}
}
private _onFailure(error: any){
// code for failure
}
}
service.spec.ts
import { TestBed, tick } from "@angular/core/testing";
import { of } from "rxjs/internal/observable/of";
import { Observable } from "rxjs/internal/Observable";
describe("Rest", () => {
let _service: RestServices;
beforeEach(() => {
const httpSpyObject = jasmine.createSpyObj("HttpClient", {
get: of(),
post: of(new HttpResponse({ status: 200, body: "Text"})),
put: of(),
});
TestBed.configureTestingModule({
providers: [
RestServices,
{ provide: HttpClient, useValue: httpSpyObject }
],
});
_service = TestBed.inject(RestServices);
});
it("should create", () => {
expect(_service).toBeTruthy();
});
it("should call Success method on success response", fakeAsync(() => {
const completeSpy = spyOn<any>(_service, "_onSuccess");
const errorSpy = spyOn<any>(_service, "_onFailure");
const subscrieSpy = spyOn(Observable.prototype, "subscribe");
const body = { name: "abc" };
_service.post(body);
tick(1000);
// Passing the below test case.
expect(subscrieSpy).toHaveBeenCalled();
// not passing below test case
expect(completeSpy).toHaveBeenCalled();
}));
});
现在,我如何通过第二个测试用例来检查 _onSuccess() 是否已从订阅中调用?
【问题讨论】:
-
我会使用
HttpClientTestingModule来测试只进行http 调用的服务:angular-training-guide.rangle.io/testing/services/http/…。 -
是的。但是在文档中。当进行任何调用时,它只会在该测试用例中被订阅。就我而言,组件方法(post)不返回任何内容。那已经订阅了数据。那么,@AliF50 现在如何检查是否调用了成功方法?
-
您可以刷新 API 调用的响应并查看调用了
completeSpy。onComplete是私有的,因此很难窥探到它。 -
@AliF50 我用flush检查了它不起作用,只是为了测试,我还将方法从私有更改为公共,但仍然不起作用。
标签: angular unit-testing jasmine karma-jasmine