Flexmock does mocking of ActiveRecord objects if you ask it nicely, but it doesn't say much about associations.
It turns out that ActiveRecord does the same thing that Hibernate does and uses an internal proxy. So if you want a post containing a single image as an association, do this:
# Create a single image.
image = flexmock(:model, Image) { |mock|
mock.should_receive(:url).and_return('/post/10/blah.jpg')
mock.should_receive(:name).and_return('Blah Description')
}
# It's an association proxy pretending to be an array.
images = flexmock(ActiveRecord::Associations::HasManyAssociation) { |mock|
mock.should_receive(:find_by_name).and_return(image)
}
# The post itself.
post = flexmock(:model, Post) { |mock|
mock.should_receive(:object_id).and_return('10')
mock.should_receive(:images).and_return(images)
}This is a Law of Demeter violation, so the better solution may be to have a get_images method.
More info here:
Misunderstanding the Law of Demeter
Demeter's Revenger
Loose Coupling takes Tight Logic