0
看来我明白了一些错误。我有一个类为什么不调用stubbed方法?
module Spree
class OmnikassaPaymentResponse
#...
# Finds a payment with provided parameters trough ActiveRecord.
def payment(state = :processing)
Spree::Payment.find(:first, :conditions => { :amount => @amount, :order_id => @order_id, :state => state }) || raise(ActiveRecord::RecordNotFound)
end
end
end
这是Rspec的specced:
describe "#payment" do
it 'should try to find a Spree::Payment' do
Spree::Payment.any_instance.stub(:find).and_return(Spree::Payment.new)
Spree::Payment.any_instance.should_receive(:find)
Spree::OmnikassaPaymentResponse.new(@seal, @data).payment
end
end
然而,这总是抛出ActiveRecord::RecordNotFound
。我期望any_instance.stub(:find).and_return()
确保无论何时,无论我在Spree :: Payment发生的任何实例上拨打#find
,它都会返回一些内容。
换句话说:我预计stub.and_return
将避免到|| raise(ActiveRecord::RecordNotFound)
。但事实并非如此。
我的假设错了吗,我的代码?还有别的吗?
比我快! ;) – lucapette 2012-04-26 08:24:33
谢谢! FWIW:也必须调用'.should_receive(:find)'而不使用'any_instance'。 – berkes 2012-04-26 08:28:36
@berkes,是的,你是对的。 – 2012-04-26 08:31:00