1
我我的服務 的單元測試過程中面臨的一個問題,這是我的測試案例噶回調總是不確定的
it("updateUserProfile() should update user profile", async(() => {
mockBackend.connections.subscribe((connection: MockConnection) => {
let responseOpts = new ResponseOptions({ body: JSON.stringify({ username: "testuser" }) });
connection.mockRespond(new Response(responseOpts));
});
let obj = {
callback: (r) => {
expect(r.username).toBe("testuser");
}
};
spyOn(obj, "callback").and.callThrough();
service.updateUserProfile({}, obj.callback);
expect(obj.callback).toHaveBeenCalled();
}));
,這我的服務
updateUserProfile(user: Profile, callback?: Function): void {
let sub = this.http.patch(url, user, { headers: this.getHeaders() }).subscribe(r => {
callback(r);
if (sub) sub.unsubscribe();
});
}
在這種情況下,它工作正常我的spy
是返回true回調已被解僱,但我的callback
內的代碼總是未定義我也想測試響應。