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

时间:2015-05-15 17:18:46

标签: ruby-on-rails ruby rspec

我使用的是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很新。如果有更惯用的方式来编写此测试,请告诉我。

2 个答案:

答案 0 :(得分: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."

答案 1 :(得分:2)

应该是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