Sha256: ff50454e7b3a45904f9001ee31ad6baba36551a558040bcdf0eda2b250014d9c

Contents?: true

Size: 923 Bytes

Versions: 10

Compression:

Stored size: 923 Bytes

Contents

# Compares the attributes of two models or hashes, ignoring attributes generated only when saving in the db
# Example: user1.should have_same_attributes_as(User.last)
RSpec::Matchers.define :have_same_attributes_as do |expected, ignore=[]|
  match do |actual|
    ignored = ['id', 'updated_at', 'created_at'].push(ignore).flatten
    actual_attr = actual.attributes unless actual.instance_of?(Hash)
    expected_attr = expected.attributes unless expected.instance_of?(Hash)

    # filter ignored values
    actual = actual_attr.except(*ignored)
    expected = expected_attr.except(*ignored)

    # some values have to be adapted before comparing the hashes
    actual = adjust_hash_values(actual)
    expected = adjust_hash_values(expected)

    actual == expected
  end
end

def adjust_hash_values(hash)
  hash.each do |k,v|
    # Time objects are compared as string
    hash[k] = v.to_s if v.is_a?(Time)
  end
  hash
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
bigbluebutton_rails-3.0.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-2.3.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-2.2.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-2.1.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-2.0.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-1.3.0.mweb1 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-1.4.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-1.4.0.beta1 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-1.3.0 spec/support/matchers/have_same_attributes_as.rb
bigbluebutton_rails-1.3.0.beta1 spec/support/matchers/have_same_attributes_as.rb