Shane Harvie: wrapping stubba in stub_object method

Shane Harvie: wrapping stubba in stub_object method

Posted: 3월 23rd, 2007, 1:48am KST
Tags
 
The stub method in stubba gives me a warning when you stub :id (“Object#id will be deprecated; use Object#object_id”), which we seem to do fairly often with ActiveRecord objects. So we use a simple method called stub_object (which we wrote originally because we didn’t know “stub” existed):

def stub_object(params)
object = Object.new
params.each do |attr, value|
object.stubs(attr).returns(value)
end
object
end

It gives us syntax like this:


manufacturer = stub_object(:id => 5, :name => "Ford", :telephone_number => "555-888-999")
Car.any_instance.expects(:manufacturer).returns(manufacturer)

And our manufacturer object will respond to any calls to :id, :name, or :telephone_number, without the warning for :id.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: