创建新的存根方法,该方法检查Rspec中是否存在该方法
我想创建一些特殊的存根方法存根检查和存根链检查,以确保该方法存在 例如:创建新的存根方法,该方法检查Rspec中是否存在该方法,rspec,rspec-rails,stub,stubbing,Rspec,Rspec Rails,Stub,Stubbing,我想创建一些特殊的存根方法存根检查和存根链检查,以确保该方法存在 例如: #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
#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\u checkpay:true您可以在spec\u 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')
编辑:如果你想让它像存根一样工作,你可能需要修改mocka或你正在使用的匹配程序。我想这是mocka,因为any_实例方法我已经考虑过了,使用resource.new.response_to不是一个好主意?方法,它是更好的资源。方法定义了吗?方法,但我想付款。存根检查