2017-03-22 8 views
0

나는 this answer을보고 나는 failure_message_when_negated 부분을 이해하지 못했다.RSpec Matchers에서 '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 

단순한 단어로는 무엇을 의미합니까? 나는 정말로 분명하고 바보가 아닌 설명에 감사하고있다.

답변

0

나는 그것을 얻는다 고 생각한다.

# 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>