From: James Mead Date: 2007-11-13T22:18:22+09:00 Subject: Re: how to ensure signature compliance while mocking in ruby ------=_Part_23737_29638745.1194959903840 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 13/11/2007, Pradeep Gatram 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 end end def test_me foo = mock('foo') foo.responds_like(Foo.new) foo.expects(:not_bar) foo.not_bar end # => NoMethodError: undefined method `not_bar' for # which responds like # -- James. http://blog.floehopper.org http://tumble.floehopper.org [1] http://mocha.rubyforge.org [2] http://mocha.rubyforge.org/classes/Mocha/Mock.html#M000032 ------=_Part_23737_29638745.1194959903840--