Ruby + Rspec: How should I be testing attr_accessor?

前端 未结 2 1199
醉酒成梦
醉酒成梦 2021-02-05 08:28

I have a ReturnItem class.

specs:

require \'spec_helper\'

describe ReturnItem do
  #is this enough?
  it { should respond_to :chosen }
  i         


        
2条回答
  •  既然无缘
    2021-02-05 08:55

    This is an updated version of the previous answer using RSpec 3, replacing failure_message_for_should for failure_message and failure_message_for_should_not for 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
    

提交回复
热议问题