Jon Schneider
Jon Schneider

Reputation: 26973

Doing a Spy in a Mocha (Ruby) test?

My team uses Mocha for unit testing in Ruby. I'd like to use Mocha to add a "spy" on a method. That is, I'd like to write a unit test using Mocha that verifies that the code under test calls a particular method on another class, without overriding the implementation of that method.

Is that possible? If so, how can it be done?

I've tried using Mocha's expects, like:

MyModule::MyClass.expects(:my_method)

However, that overrides the implementation of my_method. In my particular situation, I need to both verify that my_method was called, and to have the actual implementation of the method execute during the test.

Upvotes: 4

Views: 262

Answers (0)

Related Questions