Created
November 6, 2010 15:28
-
-
Save rubiii/665482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "#expects" do | |
before { something.expects(:some_method) } | |
around do |example| | |
begin | |
example.run | |
rescue Mocha::ExpectationError => e | |
e.message.should include("expected exactly once, not yet invoked: SomeObject.some_method") | |
end | |
end | |
it "should fail, raise an expectation_error and still pass (weird)" do | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "#expects" do | |
before { something.expects(:some_method) } | |
it "should fail" do | |
expect { verify_mocks_for_rspec }.to raise_error( | |
Mocha::ExpectationError, | |
/expected exactly once, not yet invoked: SomeObject.some_method/ | |
) | |
teardown_mocks_for_rspec | |
end | |
end |
Hmmm...I see what you're doing. The problem is two fold:
- You're using
around
in a way that it's not meant to be used. Around is intended for cases where you want to wrap examples in a transactional block. I think you're mis-using around a bit if you're setting expectations in it. - Mocha raises it's exceptions at the end after the example has run, when RSpec calls a method provided by mocha to verify method expectations. I'm not sure when RSpec calls this in relation to the around hook: it may not call it until after the around hook is done (but then again it may).
I think what you need to do is manually call the mocha method that verifies method expectations. This is similar to how rspec-mocks' own specs work. Here is a good example.
You can do something similar by calling mocha's verification method in the expect { ... }
block. You can look at RSpec's mocha adapter to see the equivalent method to call on Mocha. It looks like it's Mocha::API#mocha_verify
.
Let me know if that doesn't work for you.
hey myron, thanks for your comment. i refactored the example to use rspec's mock methods and it seems to work fine. thanks again :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there's simply no code to wrap into a
Proc
orexpect
and use theraise_error
matcher.also if there would be some code, the
Mocha::ExpectationError
would not catched. seems like it's thrown later?!