在 RSpec 中,如何在 "expect to be true" 失败时打印自定义消息?
In RSpec, how do I print a custom message when "expect to be true" fails?
我正在使用 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_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
它认为您的消息是 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."
我正在使用 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_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
它认为您的消息是 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."