Ruby on rails - 在 RSpec 中,当"expect to be true"失败时如何打印自定义消息?



我使用的是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

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

 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)

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

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

而且,我对ruby和rspec非常陌生。如果有更习惯的方法来编写这个测试,请告诉我。

它认为您的消息是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."

应该为true

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

相关内容

  • 没有找到相关文章

最新更新