2012-04-26 93 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