Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?

前端 未结 6 2065
囚心锁ツ
囚心锁ツ 2020-12-29 19:16

I have reviewed Jasmine\'s documentation of the toHaveBeenCalledWith matcher in order to understand whether it\'s possible to pass in a regular expression for an argument, i

6条回答
  •  佛祖请我去吃肉
    2020-12-29 19:58

    After doing some digging, I've discovered that Jasmine spy objects have a calls property, which in turn has a mostRecent() function. This function also has a child property args, which returns an array of call arguments.

    Thus, one may use the following sequence to perform a regexp match on call arguments, when one wants to check that the string arguments match a specific regular expression:

    var mySpy = jasmine.createSpy('foo');
    mySpy("bar", "baz");
    expect(mySpy.calls.mostRecent().args[0]).toMatch(/bar/);
    expect(mySpy.calls.mostRecent().args[1]).toMatch(/baz/);
    

    Pretty straightforward.

提交回复
热议问题