Search code examples
ruby-on-railsrspecrspec-rails

Test that a record is a duplicate of another record in RSpec


In my Rails app tested with RSpec, I'm testing a function that calls .dup() on some records and saves the duplicates. How can I test that the newly created records are duplicates of the expected original records?

I doubt there's an assertion for this specific case. And I can always make sure that the duplicate and original records have the same values for their attributes but are different records. I'm wondering if there's a more elegant, accepted pattern for this kind of test.


Solution

  • You can use the have_attributes assertion.

    match_attributes = first_user.attributes.except(:id, :created_at, :updated_at)
    
    expect(second_user).to have_attributes(match_attributes)