A Place for my head

On Ruby, Rails, Concurrency and fiction

Posted in : rails, ruby | 2 comments

This post is not about pork and coffee. So, stay clear, if google has landed you here thinking I am going to describe some sort of recipe for making nice mocha coffee with chunky bacons.

Its about using shiny new testing library called Bacon by Chris. Mocha is of course, venerable mocking library for Ruby and Rails.Here is a tiny bit of code that will make you started with bacon and mocha:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require "rubygems"
require "bacon"
require "mocha/standalone"
require "mocha/object"
class Bacon::Context
  include Mocha::Standalone
  alias_method :old_it,:it
  def it description,&block
    mocha_setup
    old_it(description,&block)
    mocha_verify
    mocha_teardown
  end
end

Thats it. Happy baking.

2 Responses to 'Making Ruby Bacon play with Mocha'

Subscribe to comments with RSS or TrackBack to 'Making Ruby Bacon play with Mocha'.

  1. Thanks very much.

    You should try and get this merged into Mocha :-)

    Rich

    12 Aug 08 at 7:05 am

  2. The above script could be improved slightly: as it stands, any mocha errors won’t cause the Bacon test to fail; they’ll just dump the errors to stdout.

    You could refactor it to the following, which avoids this issue:

    class Bacon::Context
    include Mocha::Standalone
    def it_with_mocha description
    it_without_mocha description do
    mocha_setup
    yield
    mocha_verify
    mocha_teardown
    end
    end
    alias_method_chain :it, :mocha
    end

    Rich

    12 Aug 08 at 7:28 am

Leave a Reply