我有一个这样的方法:
def self.method
#API CALL
end
我正在为调用此静态方法的控制器方法编写测试。是这样的:
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
并且运行良好。但我必须使这个方法不是静态的,并且测试开始失败。
在我的控制器中,我现在调用这样的方法:
Order.new.process_payment(@order)
我猜问题出在我的模拟上,但我不知道如何解决它。关于如何使我的模拟适应这种新格式有什么想法吗?
您可以使用
allow_any_instance_of
方法:
allow_any_instance_of(Order).to receive(:process_payment).and_return({})
伊戈尔的回答很好。我也设法让它像这样工作:
Order.any_instance.should_receive(:process_payment).and_return({})
allow_any_instance_of
已弃用。
尝试类似:
let(:order_class) {
class_double(Order).as_stubbed_const
}
before do
allow(order_class).to receive(:process_payment).and_return({})
end