【问题标题】:What does 'failure_message_when_negated' mean in RSpec Matchers?RSpec 匹配器中的“failure_message_when_negated”是什么意思?
【发布时间】:2017-03-22 07:00:58
【问题描述】:

看了this answer,没看懂failure_message_when_negated部分。

RSpec::Matchers.define :have_attr_accessor do |field|
  match do |object_instance|
    object_instance.respond_to?(field) &&
      object_instance.respond_to?("#{field}=")
  end

  failure_message do |object_instance|
    "expected attr_accessor for #{field} on #{object_instance}"
  end

  failure_message_when_negated do |object_instance|
    "expected attr_accessor for #{field} not to be defined on #{object_instance}"
  end

  description do
    "assert there is an attr_accessor of the given name on the supplied object"
  end
end

用简单的话来说是什么意思?我真的很感谢清晰而不是 foo-bar 的解释。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails


    【解决方案1】:

    我想我明白了。

    # if the Model has `attr_accessor` named `:promotion_type`
    # `it { should_not have_attr_accessor :promotion_type }` test
    # will return failure (call this `failure_message_when_negated`)
    # because the Model is clearly has `:promotion_type`
    failure_message_when_negated do |object_instance|
        "expected attr_accessor for #{field} not to be defined on #{object_instance}"
    end
    

    示例失败消息:

    1) Promotion should not assert there is an attr_accessor of the given name on the supplied object
         Failure/Error: it { should_not have_attr_accessor :promotion_type }
           expected attr_accessor for promotion_type not to be defined on #<Promotion:0x007fd939c840c8>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      相关资源
      最近更新 更多