chaijs / sinon-chai

Extends Chai with assertions for the Sinon.JS mocking framework.
Other
1.09k stars 108 forks source link

alwaysCalledWithExactly is not a function #139

Open hovissimo opened 5 years ago

hovissimo commented 5 years ago

My error: (...).to.have.been.alwaysCalledWith is not a function

My code

 it("fires props.togglePurchasable with id when clicked", function() {                     
   const props = cloneDeep(baseProps);                                                     

   const wrapper = shallow(<StoreFrontPurchasable {...props} />);                          

   wrapper.find("button").simulate("click");                                               
   wrapper.find("button").simulate("click");                                               
   wrapper.find("button").simulate("click");                                               

   expect(props.togglePurchasable.callCount).to.equal(3);                                  
   expect(props.togglePurchasable).to.have.been.alwaysCalledWithExactly(props.purchasable.id);    
 });                                                                                       

My versions: chai: 3.5.0 sinon:7.2.2 sinon-chai:3.3.0

I get the same result when I try to use alwaysCalledWith, but I don't have any problem using calledWith or calledWithExactly (except of course that they're not the assertion I need).

I noticed that alwaysCalledWith and alwaysCalledWithExactly are missing from the bottom of lib/sinon-chai.js, is this a coincidence? alwaysCalledWith and alwaysCalledWithExactly are listed in the README, so I assume they should be available.

https://github.com/domenic/sinon-chai/blob/fb4f82a684db5aad277163943ae4c6732b5d7a22/lib/sinon-chai.js#L131-L148

doughive commented 5 years ago

I found this too, it looks like the README is misleading. For me, the following syntax works expect(props.togglePurchasable).to.have.been.always.calledWithExactly(props.purchasable.id);

cincodenada commented 2 years ago

I think the README is correct, but it's perhaps not totally obvious unless you read the right-hand side of the table more closely. Here are just the relevant rows from the README:

Sinon.JS property/method Sinon–Chai assertion
alwaysCalledWithNew spy.should.always.have.been.calledWithNew
alwaysCalledWith spy.should.always.have.been.calledWith(...args)
alwaysCalledWithExactly spy.should.always.have.been.calledWithExactly(...args)
alwaysCalledWithMatch spy.should.always.have.been.calledWithMatch(...args)
alwaysReturned spy.should.have.always.returned(returnVal)
alwaysThrew spy.should.have.always.thrown(errorObjOrErrorTypeStringOrNothing)

The correct sinon-chai assertion is listed in the right-hand column, so I imagine this issue is from quickly skimming the table and trying to use the name in the left-hand column. That's understandable since always and not are the only cases where the method name is not just tacked on the end, but it of course won't work.

I've opened #153 to add a clarifying note to call this out, I think that's about all that is to be done here.