1

我想创建一些特别的存根方法stub_checkstub_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)

4

1 回答 1

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')

编辑:如果您希望它像存根一样工作,您可能需要修改 mocka 或您正在使用的匹配器(我猜它是 mocka 因为“any_instance”方法)

于 2013-01-21T15:40:42.110 回答