Close and Go BackBack to Viget

Slides and Code from “Mockfight!” Talk

Patrick Reagan
Patrick Reagan, Development Director, July 19, 2007 0

As promised, here are the slides from last night’s talk:

Since the code in the presentation is taken somewhat out of context, I’m including the full code sample as well.

Even though it ended in a tie, it may not be as bad as “kissing your sister”. Both are great libraries that provide functionality that was never possible for us when writing PHP code.  Choosing the right one is a matter of personal preference – right now, our preference is Mocha.

Mocking at NovaRUG Tomorrow

Patrick Reagan
Patrick Reagan, Development Director, July 17, 2007 0

On the heels of Mark’s contribution to the deployment panel discussion, I’ll be doing a quick talk on mocking libraries in Ruby at tomorrow’s NovaRUG meeting

Back in January, I was blown away by the demo of FlexMock that Jim Weirich gave during his “Red, Green, Refactor” talk at the Rails Edge conference.  Since then, we have been making extensive use of both FlexMock and Mocha when we need to control the behavior of objects inside the code we’re testing.  This is especially useful when our models rely on external resources (e.g. resources fetched via HTTP), but it also comes in handy when we want to hone in on testing a single piece of code:

class Die

def roll
rand(6) + 1
end
end

In our tests, we can stub out the behavior of Kernel#rand so that we’re working with a defined state (using FlexMock):

require ‘rubygems’
require ‘test/unit’
require ‘flexmock’
require ‘flexmock/test_unit’
require ‘die’

class DieTest < Test::Unit::TestCase def test_roll_when_rand_returns_zero_should_return_one die = Die.new flexmock(die).should_receive(:rand).once.with(Integer).and_return(0) assert_equal 1, die.roll end end

I’ll be testing a simple bit of Ruby code and comparing both libraries side-by-side to discuss their usage.  Mocking is a concept that I’ve been interested in for a while (and have been writing about more and more), so I’m really looking forward to the event.  Hope to see you there.

A Development Community for Viget Labs and Beyond

Every team member here at Viget Labs strives to be an innovator. We members of the development team are no different - that's why we're constantly engaging in community discussions and exploring the unknown that is the next generation of open-source web applications.

Viget Is Hiring!

Viget has job openings for Ruby Developers, Interns, and Front-End Developers. Learn More »

Recent Comments

Interesting.

I’ve been (mis)using similar behaviour in javascript for years.


var i = 0, car;
while( car = cars[i++]){
// do stuff
}

I suppose that the reason it works is exactly the same reason it works in Ruby ... but in this case I think the code is actually very easy to read.