我想创建一些特殊的存根方法stub_check
和stub_chain_check
,以确保方法存在。
例如:
#spec/controllers/payments_controller_spec.rb`
describe PaymentsController do
it "makes a payment" do
# Ensure method exists, Payment.new.respond_to?(:pay)
# the API of Payment can change and tests will pass
raise "Stubbing wrong method Payment#pay method doesn't exists" unless Payment.new.respond_to?(:pay)
Payment.any_instance.stub(pay: true) # We can stub method now
# Code...
end
end
但我想要Payment.stub_check(pay: true)
答案 0 :(得分:0)
您可以在spec_helper.rb文件上创建帮助程序:
def stub_check(resource, method, value, message)
raise message unless resource.new.respond_to?(method)
resource.any_instance.stub(method => value)
end
然后用
来调用它stub_check(Payment, :pay, true, 'Stubbing wrong method Payment#pay method doesn't exists')
编辑:如果你想让它像stub一样工作,你可能需要修改mocka或你正在使用的匹配器(我猜它是因为“any_instance”方法而被模拟)