berkes
berkes

Reputation: 27543

Why is the stubbed method not called?

It seems I understood something wrong. I have a class

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

Which is specced in Rspec:

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

This, however, always throws ActiveRecord::RecordNotFound. I expected any_instance.stub(:find).and_return() to make sure that whenever, wherever I call a #find on whatever instance I happen to have of Spree::Payment, it returns something.

In other words: I would expect the stub.and_return would avoid getting to || raise(ActiveRecord::RecordNotFound). But it does not.

Is my assumption wrong, my code? Something else?

Upvotes: 0

Views: 210

Answers (1)

KL-7
KL-7

Reputation: 47608

In your case find is not an instance method, but a class method of Spree::Payment. That means you should stub it directly without any_instance like that:

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

Upvotes: 2

Related Questions