我有一个Currency类,想要更新它的费率。以下是我打算写的更新类的规范:
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
如果我只在实际课程中更新GBP,它就可以找到:
class WebCrawlers::Currency::FeedParser
def perform
Currency.find_by_name('GBP').update_attributes(rate_to_usd: 0.63114)
end
end
然而,一旦我开始更新其他货币如'CAD',Rspec会抱怨
<Currency> received :find_by_name with unexpected arguments
expected: ("USD")
got: ("CAD")
为什么会这样?它不是不期望美元,而是说它是。
将来会有很多货币要更新,但我不想测试和存储每一种货币。我该如何解决这个问题?