Mocha Quickstart

Posted by James Mead Fri, 01 Sep 2006 14:07:00 GMT

I really must get round to writing some better documentation for Mocha, but in the meantime here’s a quickstart guide1.

Mocha adds a couple of new methods to all objects and classes – expects which sets up auto-verified expectations and stubs which stubs the method allowing any number of calls.

Both the expects and stubs methods actually return an expectation object. Relevant methods on an expectation are: at_least, at_least_once, never, raises, returns, times, with, yields which are hopefully fairly self-explanatory. If not there are some clues here

Mocking a class method
  product = Product.new
  Product.expects(:find).with(1).returns(product)
  assert_equal product, Product.find(1)
Mocking an instance method on a real object
  product = Product.new
  product.expects(:save).returns(true)
  assert product.save
Stubbing instance methods on real object
  prices = [stub(:pence => 1000), stub(:pence => 2000)]
  product = Product.new
  product.stubs(:prices).returns(prices)
  assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
Stubbing an instance method on all instances of a class
  Product.any_instance.stubs(:name).returns('stubbed_name')
  product = Product.new
  assert_equal 'stubbed_name', product.name
Traditional mocking
  object = mock()
  object.expects(:expected_method).with(:p1, :p2).returns(:result)
  assert_equal :result, object.expected_method(:p1, :p2)
Shortcuts
  object = stub(:method1 => :result1, :method2 => :result2)
  assert_equal :result1, object.method1
  assert_equal :result2, object.method2

1 I wrote these examples without checking them, so there may be some typos.

Tags , , , , , , , , ,  | 3 comments

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

Mocha example

Posted by James Mead Sun, 16 Jul 2006 18:59:00 GMT

I thought I’d put the examples from the RDoc README up here if only for the syntax highlighting.

  class Enterprise

    def initialize(dilithium)
      @dilithium = dilithium
    end

    def go(warp_factor)
      warp_factor.times { @dilithium.nuke(:anti_matter) }
    end

  end

  require 'rubygems'
  require 'mocha'
  require 'test/unit'

  class EnterpriseTest < Test::Unit::TestCase

    include Mocha

    def test_should_boldly_go
      dilithium = Mock.new
      dilithium.expects(:nuke).with(:anti_matter).at_least_once
      enterprise = Enterprise.new(dilithium)
      enterprise.go(2)
      dilithium.verify
    end

  end

Tags , , , , , , , , ,  | no comments