在RSpec中,当"期望为真"失败时,如何打印自定义消息?

Dan*_*lan 2 ruby rspec ruby-on-rails

我正在使用RSpec 3.2.0并且我有这个测试:

  it "has a webhook_payload method" do
    Project.subclasses.each { |project_class|
      expect(project_class.method_defined? :webhook_payload).to be true, "#{project_class} is a Project subclass that does not have a required 'webhook_payload' method defined."
    }
  end
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它给了我这个错误:

 Failure/Error: expect(project_class.method_defined? :webhook_payload).to be true, "#{project_class} is a Project subclass that does not have a required 'webhook_payload' method defined."
 ArgumentError:
   wrong number of arguments (2 for 1)
Run Code Online (Sandbox Code Playgroud)

我找到了关于如何使用自定义错误消息的文档,除非我有错字,否则我觉得我正确地遵循了说明:https://www.relishapp.com/rspec/rspec-expectations/v/3- 2 /文档/定制消息

此测试失败时如何打印自定义消息?

另外,我对ruby和rspec很新.如果有更惯用的方式来编写此测试,请告诉我.

Dyl*_*kow 5

它认为你的消息是方法的第二个参数be,而不是to方法.

true在括号中,它应该工作,或只是使用be_true与其他答案建议.

expect(project_class.method_defined? :webhook_payload).to be(true), "#{project_class} is a Project subclass that does not have a required 'webhook_payload' method defined."
Run Code Online (Sandbox Code Playgroud)