Search
Me

Developer for Forward. Former Technical Lead at ThoughtWorks.

Recent Comments
Elsewhere
« DNS Hosting Recommendations Requested | Main | I'm not so sure I LoveFilm »
Wednesday
18Apr2007

Checking Object Equality In Mocha

I’m working on a little pet Rails project and wanted to use Mocha to isolate my controller tests a little from what is usually encouraged, especially in light of what happened last time. I figured I’d give Mocha a go.

I wanted to verify that my service was called with objects that look equal to what I was expecting. Ordinarily, it will compare instances - are they the same object. I didn’t want that, but rather wanted to test that the values of my (ActiveRecord) objects were the equal. Easy. Mocha allows with to be called with a block that is evaluated to compare the objects.

My tests look something like this

def test_should_ask_service_to_calculate_new_prices_for_booking
address = Address.new(:postcode => "W1 1QE")
booking = create_booking

OrderBookingService.expects(:calculate_price).with(booking, address) do |b, a|
b.attributes == booking.attributes && a.attributes == address.attributes
end

post :calculate_price, :booking => booking, :collection_address => address
end

It’s something I’ve had to do many times in the past with JMock and NMock - I want to test equality for an aspect of some other object but it’s always meant writing rather a lot of code. Neat.

Reader Comments (2)

Your issue reffering to this topic supposes to be fantabulous and some scholars will easily utilize it for their customized essay or custom research papers. I do think that is a really bright idea for guys which don’t really have enough time to buy essay paper. But for me, this is more comfortable to utilize an assistance of the research paper writing service.

January 21, 2010 | Unregistered CommenterPiperRc22

I opine that you do a great contribution writing your good knowledge. After this the research writing services will be able to use it for composing the custom sociology essays. Thus different students would purchase term papers faster.

February 8, 2010 | Unregistered CommenterfIEllen

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>