Mocks in Ruby


Mocks and Stubs are one of the areas in which many developers new to testing fail to grasp. Sure i know they are fake objects which can be used for my tests, but how do they work, how can i use them? I’ve had people come up to me and ask for an early copy of just the mocks chapter of Testing Rails. Unfortunately it’s not been written yet, i’m still sifting through the various papers on the subject to make sure that i understand it before i try and explain it to other people.

Luckily people don’t have to wait for the rough cut to come out, James Mead who wrote Mocha has a nice summary slide deck on An Introduction to Mock Objects in Ruby. It’s got a heavy Mocha focus, but it’s good for everybody who wants a quick summary of the topic.

For people wondering about which mock library to use in ruby, there’s Flexmock which was written by Jim Weirich and then there is Mocha. Flexmock is the library which rails uses, and it’s been around for quite a while. It works well but the interface is a little clunky. Mocha is magical, it does things for you with a nice mocking DSL (domain specific language), and all that jazz which makes ruby so sexy.

So until i finish my chapter on mocks, check out James’ slides.


3 Comments, Comment or Ping

  1. There is also a rails plugin called “notamock”, which is an attempt to make mocking work “the ruby way” (using a DSL).

    Check it out:
    http://notamock.rubyforge.org/

    Most of the mock libraries for ruby are based on the Java mocking libraries, but notamock uses the coolness of ruby to cleanly separate the stubbing and the mocking.

    It’s pretty new, but it looks really nice.

    July 30th, 2007

  2. What are the possibilities of writing a Predictive Dialer by implementing Asterisk, Ruby and RAGI ?
    Lots of call cneter could benefit from such project and perhaps donate funds for the project.

    July 30th, 2007

  3. rabble

    Yes, omar, such a thing is buildable, but i don’t like getting those kinds of calls so why would i want to build it?

    July 30th, 2007

Reply to “Mocks in Ruby”