Sunday, December 26, 2010

Ruby Mocks vs Stubs - CleRB Presentation

I talked to somebody a while ago about a line of code that had tremendous beauty in my eyes:
setup_response = stub(:token =>'xyz_some_token')
This might seem strange to someone new to dynamic languages. The stub object returns the string 'xyz_some_token' when the "token message" is sent to it. I have no idea - and I don't really care - what type of object it is. What really matters is that is has a canned response for the "token message".

He suggested that I should do a talk on this. I submitted the idea to our local Ruby user group and Michael "Doc" Norton - the user group organizer - asked me to present it.

Preparing for a presentation is hard - takes time and effort - but I learned so much from it that I would and I will do it again!

I used the Order - Warehouse example from Martin Fowler's Mocks Aren't Stubs writing. I also wrote a Twitter client where I used mocking/stubbing in the controller tests and Fakeweb to stub out http calls from Cucumber.

After the talk we had the following conclusions:
* Although Stubs are not as sophisticated as mocks, they are really powerful and reflect clean code
* Try to use stubs over mocks
* Abused mocking could be a code smell -> introduce abstraction and use stubs

The examples from the talk are in my github repository.


I'd like to thank Joe Fiorini for meeting with me a couple of days before my talk. He had great ideas that I used in my presentation. Thanks for it!

No comments:

Post a Comment