Mocks on Rails

Posted by James Mead Thu, 31 Aug 2006 04:25:00 GMT

It’s great to see that Gluttonous has been playing with Mocha. Like many people, he’s found the ability to mock or stub class methods particularly useful – and this is one of the key differentiators between Mocha and other Ruby mocking libraries.

He’s been trying to improve the test coverage for Rails and submitted this patch where there is an interesting discussion1 about why he would prefer to use Mocha. Interestingly, there has also been some recent discussion on the RSpec mailing list about adding Mocha-like functionality.

On a different note, in his article Mocks for Speed, Gluttonous draws attention to one of the advantages I have previously mentioned of using mocks extensively to write unit tests that test a class in isolation – namely a fast build.

1 We have now released Mocha under the MIT license so it can be used for testing within Rails.

Tags , , , , , , , , , , ,  | 4 comments

Comments

  1. Maruis Marais said about 2 hours later:

    Thanks for the link to Mocha.

    Interesting you mention rSpec. Have you played with the rSpec framework? If you have, how did you find it?

  2. Labrat said about 3 hours later:

    Great work. I noticed Kevin’s post as well. I’ve been looking more closely at mocha after seeing how fast the tests run. (BTW, now that I have your plugin, your Time.now mock works flawlessly it was a problem with how I required it as suspected)

    Unfortunately, my grasp of these things is shaky at best. I would very much like to rip out all my fixtures from tests before it becomes an issue later.

    How do you use mocks to test associations such as has_many and such?

    It’s a shame I can’t figure this out myself but I’ll be looking at the plugin code for the mean time.

    Thank you and the team for opening my eyes to the world of mocks!

  3. James Mead said about 3 hours later:

    Maruis: I have played with RSpec. I really like the way the assertions work – you get much better error messages than with standard Test::Unit::Assertions. Although the mocking functionality is good and has its origins in the same JMock roots as Mocha, it doesn’t yet have the ability to mock and stub concrete classes and objects in the same way as Mocha. Like Jon Tirsen I’m still not totally convinced of the BDD aspect – I’m still into TDD.

  4. James Mead said 1 day later:

    Labrat: It depends what exactly you want to test, but you can do things like this…

      class Product < ActiveRecord::Base
        has_one :image
        has_many :prices
      end
    
      def test_me
        product = Product.new
        image = stub(:url => 'http://www.mysite.com/images/very_nice_image.gif')
        prices = [stub(:pence => 1000), stub(:pence => 2000)]
        product.stubs(:image).returns(image)
        product.stubs(:prices).returns(prices)
        # test behaviour of product here
      end

    One thing to watch out for is trying to set associated objects to be mocks – currently this doesn’t work because ActiveRecord requires them to be of the correct type. So you can’t do something like this…

      Product.new(:image => mock()) # => ActiveRecord:​:​AssociationTypeMismatch

Comments are disabled