2012-04-26 92 views
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)。但事实并非如此。

我的假设错了吗,我的代码?还有别的吗?

回答

2

对于您的情况,find不是实例方法,而是类别方法Spree::Payment。这意味着你应该直接存根没有any_instance那样:

Spree::Payment.stub(:find).and_return(Spree::Payment.new) 
+1

比我快! ;) – lucapette 2012-04-26 08:24:33

+0

谢谢! FWIW:也必须调用'.should_receive(:find)'而不使用'any_instance'。 – berkes 2012-04-26 08:28:36

+0

@berkes,是的,你是对的。 – 2012-04-26 08:31:00