Thursday, December 14, 2006

Get the edge, that is edgemocha

I had read over the past month, or so, a few blog posts on stubba and mocha. I especially appreciated Jay Fields coverage in his blog and Chris at err.the-blog. Here are some links that will get you started.

So, all that reading made me want to get immediately started with Mocha. When the time came to put it to work for me I installed the gem only to discover that at least version 0.3.2 did not appear to be working. The first surprise was that test/unit had to be required, or loaded first. But, even though that eliminated the unknown constant Test error, mocha still did not work. I am avoiding detailing the errors, etc, because their usefulness has expired. Why? Take a look if you dare at the head revision. I found the mocha team has been busy refactoring, apparently removing the test/unit dependency, and possibly other improvements.

I decided maybe I should try the head revision, that is go to the edge. I did not want to go back in versions as I was not sure how far I might need to and I thought going forward might allow me the chance to help with the project in some fashion. After following the directions at the project rubyforge site I launched an irb session to test.
irb(main):001:0> require 'mocha_standalone'
=> true
irb(main):002:0> include Mocha::SetupAndTeardown
=> Object
irb(main):003:0> setup_stubs
=> #<Mocha::Central:0xb791ac04 @stubba_methods=[]>
irb(main):004:0> $stubba
=> #<Mocha::Central:0xb791ac04 @stubba_methods=[]>
irb(main):005:0> class Foo
irb(main):006:1> end
=> nil
irb(main):007:0> foo = Foo.new
=> #<Foo:0xb7913f30>
irb(main):008:0> foo.expects(:nil?).returns("hello, mocha")
=> #<Mocha::Expectation:0xb790c384>
irb(main):009:0> foo.nil?
=> "hello, mocha"
irb(main):010:0> foo.nil?
=> "hello, mocha"
irb(main):011:0>

As you can see from the irb session you need to invoke the setup_stubs module function, or you will get an error about "stub" method not found on NilClass ($stubba). Once that is done, it appeared things were working. I wish I could say edgemocha is ready to go, but I am not sure, and hope to find out soon. Leave a comment if your experience was different, etc.

UPDATE:
Since it was requested, here is the problem I was having with 0.3.2, my apologies for any confusion. Compare:
~$ sudo gem install mocha
Attempting local installation of 'mocha'
Successfully installed mocha-0.3.2
~$ irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'test/unit'
=> true
irb(main):003:0> require 'mocha'
=> false
irb(main):004:0> Object.methods.include? "expects"
=> false
irb(main):005:0>

Now using the head revision...
~$ irb
irb(main):001:0> require 'mocha'
=> true
irb(main):002:0> include Mocha::SetupAndTeardown
=> Object
irb(main):003:0> setup_stubs
=> #<Mocha::Central:0xb78bf49c @stubba_methods=[]>
irb(main):004:0> Object.ex
Object.expects Object.extend
irb(main):004:0> Object.methods.include? "expects"
=> true
irb(main):005:0>

Shortly after I discovered requiring stubba resolved my 0.3.2 issues it appears. Oddly examples I have seen only have a require 'mocha', for example: Start Trek Example

4 Comments:

Anonymous Anonymous said...

Thanks for your interest. I think you've given us more of an incentive to get another release out there.

Can you give me details of what didn't work in release 0.3.2? This release has been out there for quite a while now without anyone else having problems.

Or was the only problem that it didn't work without Test::Unit? I guess it doesn't say so explicitly in the documentation, but if you look at all the examples, this version was only ever intended to work in conjunction with Test::Unit.

You are correct that HEAD now separates itself from Test::Unit. This was mainly work done to help out the RSpec developers who at one stage wanted to integrate Mocha as the mocking component for RSpec. However they now have their own (very similar) mocking component.

I can't really imagine why you would want to use Mocha outside a testing framework. Your example illustrates a couple of the problems of using Mocha outside a testing framework - when do expectations get verified and when do stubbed classes get put back to their original state. You need to decide when to trigger calls to e.g. verify_stubs() and teardown_stubs().

3:54 AM  
Blogger Johnny P said...

Thanks for the feedback and I added an update with the problems I found.

3:50 PM  
Anonymous Anonymous said...

The Star Trek example only uses traditional-style mock objects - calling the mock() method to create them. In this case (with the 0.3.2 gem) it only needs to require 'mocha'.

In your example you are focussing on setting expectations on real (non-mock) objects. In this case (with the 0.3.2 gem) you need to require 'stubba'. As in this other example.

2:15 AM  
Blogger Johnny P said...

Thanks James. I jumped in to this without really getting the difference between mochs and stubs. It was not sinking in until now.

3:42 PM  

Post a Comment

<< Home