Examine one particular call and ignore the rest

Posted by lulalala on Stack Overflow See other posts from Stack Overflow or by lulalala
Published on 2011-11-21T01:45:15Z Indexed on 2011/11/21 1:51 UTC
Read the original article Hit count: 188

Filed under:
|

I have a Currency class and want to update its rates. The following is the spec of an update class I plan to write:

describe WebCrawlers::Currency::FeedParser do
  let(:gbp){ double('GBP').as_null_object }
  let(:usd){ double('USD').as_null_object }
  describe '#perform' do
    before do
      Currency.stub(:find_by_name).with('GBP').and_return( gbp )
      Currency.stub(:find_by_name).with('USD').and_return( usd )
    end

    it 'should update GBP rate' do
      gbp.should_receive(:update_attributes).with(rate_to_usd:0.63114)
      subject.perform
    end

    it 'should not update USD rate' do
      usd.should_not_receive(:update_attributes)
      subject.perform
    end
  end
end

and it works find if I only update GBP in my actual class:

class WebCrawlers::Currency::FeedParser
  def perform
    Currency.find_by_name('GBP').update_attributes(rate_to_usd: 0.63114)
  end
end

However once I start updating other currencies like 'CAD', Rspec complains

   <Currency> received :find_by_name with unexpected arguments
     expected: ("USD")
          got: ("CAD")

Why is this the case? Instead of NOT expecting USD, it says it is.

And in the future there will be lots of currencies to update, but I don't want to test and stub each one of them. How can I resolve this issue?

© Stack Overflow or respective owner

Related posts about rspec

Related posts about rspec2