Injecting mocks (the Mocha way)

Posted by James Mead Fri, 01 Sep 2006 06:26:00 GMT

I thought I’d compare and contrast the way Gluttonous was injecting mocks and how you can do it with Mocha.

Gluttonous’ way

  def test_process_exit
    delegate_methods_to_mock!(RailsFCGIHandler, :close_connection) do
      fcgi = flexmock()
      fcgi.should_receive(:close_connection)
      @handler.mock = fcgi
      @handler.stubs(:when_ready).returns(:exit)
      @handler.process!
    end
  end

Using Mocha

  def test_process_exit
    @handler.expects(:close_connection)
    @handler.stubs(:when_ready).returns(:exit)
    @handler.process!
  end

I think it’s a bit more readable and you don’t need the block construction which starts becoming a nuisance when you need to stub methods on multiple classes.

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

Comments

  1. Kevin Clark said about 7 hours later:

    Oh seriously? Damn it, now I have to try and push mocha in there instead of flexmock. You’re just giving me all sorts of problems this week James.

  2. James Mead said about 7 hours later:

    Yup. Sorry – I really need to write up some better documentation :-)

    I’ll try and write a quickstart article…

  3. Zero said 242 days later:

    no bad James.

  4. Zero said 242 days later:

    no bad James.

Comments are disabled