Gabriel Mesquita
Gabriel Mesquita

Reputation: 2411

Rspec test with mock for a static method and non static method

I have a method like this:

def self.method
  #API CALL
end

And I was writing a test for the controller method that calls this static method. It is like this:

it 'update order to confirmed' do
    Order.should_receive(:process_payment).and_return({})
    sign_in user
    attributes = FactoryGirl.attributes_for(:order, :valid_order)
    patch :confirm_order, params: { id: order.id, order: attributes }
    order.reload
    expect(order.confirmed).to eq true
end

And it was working fine. But I had to make this method not static, and the test starts to fail.

In my controller, I am calling the method like this now:

Order.new.process_payment(@order)

The problem is with my mock I guess, but I cannot see how to solve it. Any ideas on how I can adapt my mock to this new format?

Upvotes: 8

Views: 9125

Answers (2)

Gabriel Mesquita
Gabriel Mesquita

Reputation: 2411

Igor's answer works fine. I've also managed to make it work like this:

Order.any_instance.should_receive(:process_payment).and_return({})

Upvotes: 0

Igor Drozdov
Igor Drozdov

Reputation: 15045

You can use allow_any_instance_of method:

allow_any_instance_of(Order).to receive(:process_payment).and_return({})

Upvotes: 5

Related Questions