Tuesday, February 12, 2013

RSpec test failing when testing for parameters using "should_receive" and "with"

So, another weird and frustrating thing happened to me while i was running my app's tests using rspec.
I got 1 failing spec showing the following error:


Spec::Mocks::MockExpectationError in 'FeedsController#get_recent_feeds gets the recent feeds for live deals'

#[Feed:0x3fa7ffc8de1c @name="Feed_1001"] received :all with unexpected arguments
  expected: ({:include=>:feedable,  :order=>"updated_at desc, feedable_type desc"})
       got: ({:include=>:feedable, :order=>"updated_at desc, feedable_type desc"})


The hashes being exactly the same, i was baffled why the test failed.

I tried a few things but to no avail. I was frustrating allright, with the release deadline approaching and you wasting your time with a stupid test case.

After googling(a lot of it) i came across this beautiful method hash_including. 

What you just have to do is, in the with method, where you are passing the parameters which you want to test, use

hash_including(:params => :to_test)

and thats it. In my case what i did was

@feeds.should_receive(:all).with(hash_including(:include => :feedable, :order => 'updated_at desc, feedable_type desc')).and_return(@feeds)

And then everything worked out. :)

No comments:

Post a Comment