admin管理员组

文章数量:1026989

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.

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.

本文标签: javascriptTypescript Jasmine toHaveBeenCalledTimes() is not a functionStack Overflow