2016-08-23 86 views
0

我在我的打字稿應用測試:打字稿茉莉花toHaveBeenCalledTimes()不是一個函數

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,沒有錯誤。但是,當我運行它,我有以下錯誤:SpyObj的

TypeError: expect(...).toHaveBeenCalledTimes is not a function

如何測試,多少次函數被調用? 在此先感謝。

回答

1

看看here,並進入的「其他屬性跟蹤」

您可以嘗試使用.calls.count()財產部分。

所以你的測試變爲: expect(subscriber.processNotifyAction.calls.count()).toEqual(2)

旁註 - 當然,這是假設你的茉莉花的版本都支持此,它應該,除非你有一個非常老版本的茉莉花。

+0

謝謝,您的解決方法適用於我。還有一件事,我從CreateSpyObject 切換到SpyOn,然後使用「.calls.count()」。 –