How to write a test which expects an Error to be thrown in Jasmine?

前端 未结 9 1046
忘了有多久
忘了有多久 2020-11-28 17:26

I\'m trying to write a test for the Jasmine Test Framework which expects an error. At the moment I\'m using a Jasmine Node.js integration from GitHub.

In my Node mod

相关标签:
9条回答
  • 2020-11-28 18:20

    For coffeescript lovers

    expect( => someMethodCall(arg1, arg2)).toThrow()
    
    0 讨论(0)
  • 2020-11-28 18:20

    In my case the function throwing error was async so I followed here:

    await expectAsync(asyncFunction()).toBeRejected();
    await expectAsync(asyncFunction()).toBeRejectedWithError(...);
    
    0 讨论(0)
  • 2020-11-28 18:27

    Try using an anonymous function instead:

    expect( function(){ parser.parse(raw); } ).toThrow(new Error("Parsing is not possible"));
    

    you should be passing a function into the expect(...) call. Your incorrect code:

    // incorrect:
    expect(parser.parse(raw)).toThrow(new Error("Parsing is not possible"));
    

    is trying to actually call parser.parse(raw) in an attempt to pass the result into expect(...),

    0 讨论(0)
提交回复
热议问题