【问题标题】:How to call toHaveBeenCalledWith with multiple parameters?如何使用多个参数调用 toHaveBeenCalledWith?
【发布时间】:2018-03-29 04:49:42
【问题描述】:

您好,我正在Angular JS 开发 Web 应用程序。我正在使用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"));
        })
    }));

以下是我的服务。

 updateUser<T>(event: boolean, userid: string): Observable<T>{
        var updateUserResult = { result: true } as any;
        return Observable.of<T>(updateUserResult);
    }

我尝试如下调用服务,但没有任何结果。

expect(service.updateUser).toHaveBeenCalledWith(true,"101");
expect(service.updateUser).toHaveBeenCalledWith([true]["101"]);

有人可以帮我调用我的模拟服务吗?任何帮助,将不胜感激。谢谢

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    自开玩笑 23.0 以来,有 .toHaveBeenNthCalledWith(nthCall, arg1, arg2, ....) https://facebook.github.io/jest/docs/en/expect.html#tohavebeennthcalledwithnthcall-arg1-arg2-

    所以你可以做出如下断言: expect(service.updateUser).toHaveBeenNthCalledWith(1, true,"101");

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      相关资源
      最近更新 更多