Typescript Jasmine toHaveBeenCalledTimes()不是函数

时间:2016-08-23 21:00:29

标签: javascript typescript jasmine

我在打字稿应用程序中进行了测试:

    it("Should send current state when new subscriber is added (watching over file)",
            () => {
                runs(() => {
                    flag = false;

                    subscriber = createSpyObj<IPathWatchSubscriber>("PathWatchSubscriberMock", ["processNotifyAction"]);
                    subscriber2 = createSpyObj<IPathWatchSubscriber>("PathWatchSubscriberMock", ["processNotifyAction"]);

                    pathWatch.subscribe(subscriber);
                    pathWatch.watch(filePath);
                    pathWatch.subscribe(subscriber2);
                    w(() => { flag = true; });
                });
                waitsFor((): boolean => {
                    return flag;
                }, "failure", chokidarOperationDelay);

                runs(() => {
                    expect(subscriber.processNotifyAction).toHaveBeenCalledWith(expectedNotifyAction);
                    expect(subscriber.processNotifyAction).toHaveBeenCalledTimes(2);
                    expect(subscriber2.processNotifyAction).toHaveBeenCalledWith(expectedNotifyAction);
                });
            }
        );

当我将其编译成js时,没有错误。但是当我运行它时,我有以下错误:

  

TypeError:expect(...)。toHaveBeenCalledTimes不是函数

如何测试SpyObj的功能被调用了多少次? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

查看here,然后转到“其他跟踪属性”部分

您可以尝试使用.calls.count()属性。

所以你的测试成了: expect(subscriber.processNotifyAction.calls.count()).toEqual(2)

旁注 - 这当然是假设您的Jasmine版本支持此功能,除非您有一个非常旧版本的Jasmine,否则应该这样做。

答案 1 :(得分:0)

toHaveBeenCalledTimes() was introduced in Jasmine 2.4。根据症状判断 - toHaveBeenCalledWith()没有失败,看起来你有茉莉花2.3或更早,升级。