New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shouldNot receive: passes when the message is received #486
shouldNot receive: passes when the message is received #486
Conversation
This is a great bug. Thanks for the issue, @chrisdevereux! RSpec takes the approach you describe in (2); it fails fast if the matcher is a negative clause. That is, the following spec: describe Car do
let(:car) { described_class.new }
it 'should not receive honk' do
car.should_not_receive(:honk) # or `expect(car).to_not receive(:honk)`
car.honk
car.honk
end
end Fails with the following message (notice the failure message says "received 1 time", not "2 times"):
I also initially thought (1) was better. However, if the user expects a fast-fail, but the message is sent twice and thus I have yet to examine whether Kiwi can implement (2) easily, but if you'd like to take a stab at it, I definitely think that's the way to go. |
Hey, thanks for the quick reply. Not sure I understand this point:
If "shouldNot receive" means I agree that the failure message would be a bit strange though. Maybe a better solution would be to go with the behavior described above, but move should/shouldNot into a separate class to the other |
I'm assuming here that the behavior we want is that Moving from the current behavior to that would cause currently passing tests to fail (in some case, desirably so, in some cases not), but I can't think of a case where it causes a false-positive. |
Whoops, I think I got confused while writing my first comment. Indeed you're right; my argument about the false positive was flawed--my bad!
Actually, the following spec fails in RSpec: describe Car do
let(:car) { described_class.new }
it 'should receive honk' do
car.should_receive(:honk) # or `expect(car).to receive(:honk)`
car.honk
car.honk
end
end The failure message indicates that the spec fails because
So if we wanted to emulate how RSpec behaves, we would not want I think users generally expect Kiwi to behave like RSpec does (just search this repository for the string "RSpec" and GitHub will return plenty of issues pointing out discrepancies), so I think the best thing to do here is to change how |
Ahhh, got it now. Thanks! |
@@ -28,6 +28,7 @@ | |||
- (BOOL)shouldBeEvaluatedAtEndOfExample; | |||
- (BOOL)willEvaluateMultipleTimes; | |||
- (void)setWillEvaluateMultipleTimes:(BOOL)shouldEvaluateMultipleTimes; | |||
- (void)setNegativeMatcherBehavior:(BOOL)negativeMatcherBehavior; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the intent of method would be clearer if it was named -setWillEvaluateAgainstNegativeExpectation:
or -setIsNegativeExpectation:
.
Awesome! I pulled this down and confirmed it works exactly as expected. I think this will be good to merge after addressing a few comments I left above. @supermarin @stepanhruda This fixes a bug in the NSObject *mock = [NSObject mock];
[[mock shouldNot] receive:@selector(description)];
[mock description];
[mock description]; Personally I think this can be released in a minor version like 2.2.5. Let me know if you feel differently, otherwise I'll hit the big green button. |
I would personally say that it is quite important that the sample test you quoted fails as it would be very counterintuitive otherwise. [web [iPhone msg]]
|
++ for merging after your comments are addressed. |
Argh, just spotted a problem: This issue also applies to |
Choice. Is this ready to be merged, @chrisdevereux? (Sorry for the delay, I don't get a notification when commits are added to PRs 😦) |
Sorry, should have added a comment! I'm happy with it. I didn't add this behavior to the depracated NSInvocation-based matchers (would have required more dramatic changes to the KWShouldReceiveMatcher class and presumably they're going away soon anyway). If that's a problem, I can take a look, otherwise go for it! |
shouldNot receive: passes when the message is received
If anyone wants the same behavior for the deprecated matchers they can file an issue, or preferably a pull request. I'd rather fix the supported matchers as soon as possible. Thanks for pointing out the issue, and then fixing it! 👍 |
[[object should] receive:selector]
is equivalent to[[object should] receive:selector withCount:1]
However, this means that
[[object shouldNot] receive:selector]
is equivalent to[[object shouldNot] receive:selector withCount:1]
This means that the matcher tests for the opposite of what it says it tests.
There seem to be 3 options for fixing this:
[[object should] receive:selector]
to do[[object should] receive:selector withCountAtLeast:1]
. Arguably, this is closer to what "should receive" means normally, but it's a breaking change.My preference would be for 1, but I thought I'd check what people think before starting anything.