Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 931
Gabriel Mesquita Avatar asked Aug 17 '17 13:08

Gabriel Mesquita


1 Answers

You can use allow_any_instance_of method:

allow_any_instance_of(Order).to receive(:process_payment).and_return({})
like image 154
Igor Drozdov Avatar answered Nov 07 '22 19:11

Igor Drozdov