Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建新的存根方法,该方法检查Rspec中是否存在该方法_Rspec_Rspec Rails_Stub_Stubbing - Fatal编程技术网

创建新的存根方法,该方法检查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不是一个好主意?方法,它是更好的资源。方法定义了吗?方法,但我想付款。存根检查