Sha256: 2e803f86ceb999dcb93af0b219bdbc3135207e01d867c2eae803188d236c640b

Contents?: true

Size: 1.11 KB

Versions: 22

Compression:

Stored size: 1.11 KB

Contents

RSpec::Matchers.define :be_a_new do |model_klass|
  match do |actual|
    actual.is_a?(model_klass) && actual.new_record? && attributes_match?(actual)
  end

  chain :with do |expected_attributes|
    attributes.merge!(expected_attributes)
  end

  failure_message_for_should do |actual|
    [].tap do |message|
      unless actual.is_a?(model_klass) && actual.new_record? 
        message << "expected #{actual.inspect} to be a new #{model_klass.inspect}"
      end
      unless attributes_match?(actual)
        if unmatched_attributes.size > 1
          message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
        else
          message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
        end
      end
    end.join(' and ')
  end

  def attributes
    @attributes ||= {}
  end

  def attributes_match?(actual)
    attributes.stringify_keys.all? do |key, value|
      actual.attributes[key].eql?(value)
    end
  end

  def unmatched_attributes
    attributes.stringify_keys.reject do |key, value|
      actual.attributes[key].eql?(value)
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rspec-rails-2.8.0.rc1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.7.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.7.0.rc1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.1.beta1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.0.rc6 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.0.rc4 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.6.0.rc2 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.5.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.4.1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.4.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.3.1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.3.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.2.1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.2.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.1.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.0.1 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.0.0 lib/rspec/rails/matchers/be_a_new.rb
rspec-rails-2.0.0.rc lib/rspec/rails/matchers/be_a_new.rb