嘲笑用toHaveBeenLastCalledWith嵌套函数

时间:2018-07-29 10:02:53

标签: javascript jestjs

我有这样的功能

    function sendToJava<T>(streamName: string) {
    return (source: Observable<T>) => Observable.create((subscriber: Subscriber<string>) => source.subscribe(
        (value: any) => ((window as any)[streamName] as JavaApiCall)({            
            request: JSON.stringify(value),
            onSuccess: message => {console.log("ttttttt"); return subscriber.next(message.toString())},
            onFailure: (code, message) => subscriber.error({ code, message })
        }),
        (err: any) => subscriber.error(err),
        () => subscriber.complete()
    ));
}

我想检查请求功能是否正常工作,所以我做了这样的测试

`describe('It should return tranformated stream when using sendtoJava function', () => {
  test('', () => {
    const mockFunction = jest.fn();
    const result =(window as any)['teststream'] = mockFunction;
    const subscriber = jest.fn();
    const source = of(1,2);
    const observableStream:any = source.pipe(sendToJava<Number>('teststream')).subscribe(subscriber);
    expect(mockFunction).toHaveBeenCalledTimes(2);
    let request =2;
    expect(mockFunction).toHaveBeenLastCalledWith(request,undefined,undefined);
    });
  });`

但是在我调用toHaveBeenLastCalled的行中,我收到了错误,我只想检查请求值而不是rest函数。

Error message

expect(jest.fn()).toHaveBeenLastCalledWith(expected)

Expected mock function to have been last called with:
  2
as argument 1, but it was called with
  {"onFailure": [Function onFailure], "onSuccess": [Function onSuccess], "request": "2"}.

0 个答案:

没有答案