Thursday, September 14, 2006

Mocking Ruby

No... not like it has been recently.

The question came up of whether I had been using mock objects before and how I would do it in Ruby. Having read recently Pragmatic Unit Testing, I had a few ideas on how to proceed. However, the rules change when you are using Ruby instead of Java.

Here is the test code I answered my questions with:

puts "before: #{Time.now}"

class Time
def self.now
'mock answer'
end
end

puts "after: #{Time.now}"

Outputs:

before: Thu Sep 14 13:02:46 EDT 2006
after: mock answer

No wrapper class. No factories.

For the sake of full disclosure:
  • You might want to save a pointer to the old method.

  • The way you would do that is a little cumbersome for static methods . But it's very clean otherwise.

0 Comments:

Post a Comment

<< Home