Skip to content

Instantly share code, notes, and snippets.

@rubiii
Created November 6, 2010 15:28
Show Gist options
  • Save rubiii/665482 to your computer and use it in GitHub Desktop.
Save rubiii/665482 to your computer and use it in GitHub Desktop.
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
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
@rubiii
Copy link
Author

rubiii commented Nov 6, 2010

there's simply no code to wrap into a Proc or expect and use the raise_error matcher.
also if there would be some code, the Mocha::ExpectationError would not catched. seems like it's thrown later?!

@myronmarston
Copy link

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.

@rubiii
Copy link
Author

rubiii commented Nov 7, 2010

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