Mocking instances created via ActiveRecord’s find

Most Ruby mocking frameworks have the ability to mock a new object created via a constructor. However, when an object is created via ActiveRecord’s find or find_by_* methods, the .new method isn’t invoked. Instead, the .instantiate method is called.

For example, to specify :instantiate as the object creation method using FlexMock:

flexmock(Dog).new_instances(:instantiate)
.should_receive(:bark)
.at_least.once
.and_return(:woof)

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *