toHaveBeenCalledWith does no longer work with custom matcher #536

Closed
tbuschto opened this Issue Mar 1, 2014 · 5 comments

4 participants

@tbuschto

In Jasmine 1.3 you could use your custom matcher with toHaveBeenCalledWith, which was especially useful to ensure that the actual argument was identical to the expected one, not just equal. E.g.:

  var mySpy = jasmine.createSpy();
  var foo = {};
  var bar = {};
  mySpy( foo );

  expect( mySpy ).toHaveBeenCalledWith( foo );
  expect( mySpy ).toHaveBeenCalledWith( bar );
  expect( mySpy ).toHaveBeenCalledWith( same( foo ) );
  expect( mySpy ).not.toHaveBeenCalledWith( same( bar ) );

This is no longer possible with Jasmine 2.0, only 'any' and 'objectContaining' are accepted.

@infews

This is intended behavior. Things were muddied and poorly documented in 1.x in this area. We're now making it much more explicit and easier to maintain.

It looks like there are two parts to your question.

First, toHaveBeenCalledWith() uses the equality functionality across parameters. So if you want to have custom equality, you need to add a custom equality tester. This is how any and objectContaining work in 2.0.

However, what you appear to be wanting to do is check that the specific reference is used. This sounds like you want to write a custom matcher that would be named something like toHaveBeenReallyCalledWith() (or some better name) in order to accomplish what you want to do here.

Does this help? If so, I'd like to close this issue.

@tbuschto

Thank you for the timely response. I did not know about custom equality testers. I think with those I can re-create the pattern I need. Also, in the example the tester is added in "beforeEach". Would it suffice to add it only once at startup?

@tbuschto

It seems like custom equality testers do not work with tohaveBeenCalled with, which once again would prevent me for using it to test for identity?

@thcgit

I'm also having the same issue. I have added a custom equality tester but it's not being picked up by toHaveBeenCalledWith.

@sheelc sheelc closed this in 00c8e37 Mar 27, 2014
@sheelc

Just pushed a fix where custom equality testers weren't working with toHaveBeenCalledWith. Hopefully that works on master now, but please feel to reopen if it doesn't.

@abirembaut abirembaut added a commit to bonitasoft/bonita-ui-designer that referenced this issue Oct 22, 2015
@abirembaut abirembaut BS-14459 - use same jasmine version as frontend in order to avoid this e3f8415
@abirembaut abirembaut added a commit to bonitasoft/bonita-ui-designer that referenced this issue Dec 4, 2015
@abirembaut abirembaut BS-14459 - use same jasmine version as frontend in order to avoid this e8d2d33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment