[mocha-developer] how to ensure signature compliance while mocking in ruby

James Mead jamesmead44 at gmail.com
Tue Nov 13 08:18:23 EST 2007

On 13/11/2007, Pradeep Gatram <pradeep.gatram at gmail.com> wrote:
> Let me put my dilemma as an example. Take a look at a snippet from
> FooTest.
> #using mocha
> def test_method1
>   Bar.expects(:method2).with('param1', 'param2').once
>   Foo.method1
> end
> And now the implementation
> class Foo
>   def self.method1
>     Bar.method2('param1', 'param2')
>   end
> end
> class Bar
> end
> So far so good... Now my dilemma is that while implementing method2 in
> Bar, I can come up with any weird signature (e.g. def method2
> someInteger, someOtherNumber) and I will not be breaking FooTest. This
> obviously makes life hard while refactoring. Coming from a Java/C#
> background, I used to rely on compilation to catch these issues.
> How have people solved such problems?
> Pradeep

Hi Pradeep,
Fundamentally you should never rely on only mock based tests, you should
always have some functional tests to check all the objects are wired
together correctly, but...

Mocha [1] used to only allow you to mock *existing* methods on *concrete*
classes. This functionality accidentally got removed a while ago, but I'd
like to reintroduce it asap.

You can already restrict expecations on *mocks* by using the responds_like
modifier [2], something like this...

class Foo
  def bar

def test_me
  foo = mock('foo')

# => NoMethodError: undefined method `not_bar' for #<Mock:foo> which
responds like #<Foo:0x432e5c>


[1] http://mocha.rubyforge.org
[2] http://mocha.rubyforge.org/classes/Mocha/Mock.html#M000032

More information about the mocha-developer mailing list