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

前端 未结 9 1099
忘了有多久
忘了有多久 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: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(...),

提交回复
热议问题