最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jasmine toThrowError fail, message "Error: Actual is not a function" - Stack Overflow

programmeradmin4浏览0评论

I'm building a Jasmine spec and writing toThrowError test.

 it("Should give time travel error", () => {
        const errorMsg = Logger.getLogsBetweenDates( { 
              fromDate : new Date("2017-01-06"),
              toDate : new Date("2017-01-05")});

        expect(errorMsg).toThrowError("Time travel error: fromDate must be befor toDate");
});

And im getting "Error: Actual is not a function" with no extra details.

  • What is that Actual?
  • The Logger.getLogsBetweenDates function is acutely throw the error, and the test is always fail. what am i doing wrong?

I'm building a Jasmine spec and writing toThrowError test.

 it("Should give time travel error", () => {
        const errorMsg = Logger.getLogsBetweenDates( { 
              fromDate : new Date("2017-01-06"),
              toDate : new Date("2017-01-05")});

        expect(errorMsg).toThrowError("Time travel error: fromDate must be befor toDate");
});

And im getting "Error: Actual is not a function" with no extra details.

  • What is that Actual?
  • The Logger.getLogsBetweenDates function is acutely throw the error, and the test is always fail. what am i doing wrong?
Share Improve this question edited Jan 18, 2018 at 16:37 Elad asked Jan 18, 2018 at 13:07 EladElad 2,3771 gold badge18 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

What is that Actual?

As its name suggest, the Actual is the variable that contain the actual result from the tested function. what's your tested function actually returned.

Then, Jasmine take that Actual value and compare it with your expect value. It's easier to understand when you see the source code here.

Its happen in the code becuse the Logger.getLogsBetweenDates is throw an error, and errorMsg get no result; so the Actual is undefined, and the expect function compare an undefined to the error message.

what am i doing wrong?

You need to call the tested function inside of the expect function, like so:

it("Should give time travel error", () => {    
     expect(() => {
             Logger.getLogsBetweenDates( { 
                  fromDate : new Date("2017-01-06"),
                  toDate : new Date("2017-01-05")})
            }).toThrowError("Time travel error: fromDate must be befor toDate");
});

As shown here.

发布评论

评论列表(0)

  1. 暂无评论