Nir*_*jan 5 unit-testing jasmine angularjs
您好,我正在开发 Web 应用程序Angular JS。我正在使用框架编写单元测试用例Jasmine。我试图嘲笑一些服务。我无法使用多个参数调用服务。下面是我的单元测试代码。
it('update is allowed false', async(() => {
let service = fixture.debugElement.injector.get(UserOnboardEndpointMock);
spyOn(service, 'updateUser').and.callThrough();
fixture.whenStable().then(() => {
expect(service.updateUser).toHaveBeenCalledWith(jasmine.any(true,"101"));
})
}));
Run Code Online (Sandbox Code Playgroud)
下面是我的服务。
updateUser<T>(event: boolean, userid: string): Observable<T>{
var updateUserResult = { result: true } as any;
return Observable.of<T>(updateUserResult);
}
Run Code Online (Sandbox Code Playgroud)
我已尝试如下拨打服务电话,但没有成功。
expect(service.updateUser).toHaveBeenCalledWith(true,"101");
expect(service.updateUser).toHaveBeenCalledWith([true]["101"]);
Run Code Online (Sandbox Code Playgroud)
有人可以帮我调用我的模拟服务吗?任何帮助,将不胜感激。谢谢
从 jest 23.0 开始,有 .toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....) https://facebook.github.io/jest/docs/en/expect.html#tohavebeennth Calledwithnthcall-arg1-arg2-
所以你可以做出这样的断言:
expect(service.updateUser).toHaveBeenNthCalledWith(1, true,"101");