Does Jasmine's toThrow matcher require the argument to be wrapped in an anonymous function?

前端 未结 4 495
忘了有多久
忘了有多久 2020-12-05 09:29

The documentation at https://github.com/pivotal/jasmine/wiki/Matchers includes the following:

expect(function(){fn();}).toThrow(e);

As disc

4条回答
  •  时光取名叫无心
    2020-12-05 10:11

    Sadly, it seems that if I need to test a function that takes parameters, then I will need to wrap it with a function.

    I would rather have preferred,

    expect(myTestFunction, arg1, arg2).toThrow();
    

    But I am ok with explicitly doing

    expect(function(){myTestFunction(arg1, arg2);}).toThrow("some error");
    

    FYI note that we can also use regex match on error:

    expect(function (){myTestFunction(arg1, arg2);}).toThrowError(/err/);
    

提交回复
热议问题