最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Typescript Jasmine toHaveBeenCalledTimes() is not a function - Stack Overflow

matteradmin6PV0评论

I have a test in my typescript application:

    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);
                });
            }
        );

When I pile it into js, there are no errors. But when I run it, I have following error:

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

How to test, how many times function of SpyObj was called? Thanks in advance.

I have a test in my typescript application:

    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);
                });
            }
        );

When I pile it into js, there are no errors. But when I run it, I have following error:

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

How to test, how many times function of SpyObj was called? Thanks in advance.

Share Improve this question edited Aug 23, 2016 at 21:03 Ulad Melekh asked Aug 23, 2016 at 21:00 Ulad MelekhUlad Melekh 9443 gold badges18 silver badges33 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

Take a look here , and go to the section of "Other tracking properties"

You can try using the .calls.count() property.

So your test bees: expect(subscriber.processNotifyAction.calls.count()).toEqual(2)

Side note - This is of course assuming your version of Jasmine supports this, which it should unless you have a REALLY old version of Jasmine.

toHaveBeenCalledTimes() was introduced in Jasmine 2.4. Judging by the symptoms - toHaveBeenCalledWith() did not fail, it looks like you have Jasmine 2.3 or older, upgrade.

Post a comment

comment list (0)

  1. No comments so far