Rspec 匹配器未使用自定义错误消息

发布于 2024-11-02 18:27:05 字数 658 浏览 0 评论 0原文

我有一个自定义匹配器:

RSpec::Matchers.define :have_value do |attribute, expected|
  match do |obj|
    obj.send(attribute) == expected
  end 

  description do
    "have attribute #{attribute} with value #{expected}" 
  end
end

这是我如何使用它的示例:

context "description" do
        subject { create_obj_from_file(file_name) }
        h = {
            :attribute1 => 6,
            :attribute2 => 3,
            :attribute3 => "PL" }
        }
        h.each do |k,v| it { should have_value k, v} end
    end

这正在正确运行我的测试。但是当我收到错误时,这不是自定义错误,而是“预期 {masssive object dump} 有值:属性和值”关于我做错了什么有什么想法吗?

I have a custom matcher:

RSpec::Matchers.define :have_value do |attribute, expected|
  match do |obj|
    obj.send(attribute) == expected
  end 

  description do
    "have attribute #{attribute} with value #{expected}" 
  end
end

And this is an example of how I am using it:

context "description" do
        subject { create_obj_from_file(file_name) }
        h = {
            :attribute1 => 6,
            :attribute2 => 3,
            :attribute3 => "PL" }
        }
        h.each do |k,v| it { should have_value k, v} end
    end

This is running my tests correctly. But when I get an error, it's not the custom error, it is "expected {masssive object dump} to have value :atttribute and value" Any ideas as to what I"m doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-11-09 18:27:05

感谢您使用我的代码来回答您的上一个问题。以下是此示例所需的内容:

  failure_message_for_should do |obj|
    "should have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}" 
  end


  failure_message_for_should_not do |obj|
    "should not have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}" 
  end

Thanks for using my code in response to your last question. Here is what you need for this example:

  failure_message_for_should do |obj|
    "should have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}" 
  end


  failure_message_for_should_not do |obj|
    "should not have value #{expected} for attribute #{attribute} but got #{obj.send(attribute)}" 
  end
演出会有结束 2024-11-09 18:27:05

您需要指定自定义失败消息。来自 wiki 的示例:

RSpec::Matchers.define :be_a_multiple_of do |expected|
  match do |actual|
    actual % expected == 0
  end

  failure_message_for_should do |actual|
    "expected that #{actual} would be a precise multiple of #{expected}"
  end

  failure_message_for_should_not do |actual|
    "expected that #{actual} would not be a precise multiple of #{expected}"
  end

  description do
    "be a precise multiple of #{expected}"
  end
end

You need to specify custom failure messages. An example from the wiki:

RSpec::Matchers.define :be_a_multiple_of do |expected|
  match do |actual|
    actual % expected == 0
  end

  failure_message_for_should do |actual|
    "expected that #{actual} would be a precise multiple of #{expected}"
  end

  failure_message_for_should_not do |actual|
    "expected that #{actual} would not be a precise multiple of #{expected}"
  end

  description do
    "be a precise multiple of #{expected}"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文