Jes Jes - 10 months ago 69
Ruby Question

RSpec - equality match, two different instances

I created a parser, which reads from CSV file and creates objects from every row. It works fine, but now I created Rspec tests. I have:

let(:sample_row) { '01.09.2016 08:49', service: 'International', phone_number: '48627843111', raw_duration: '0:29', unit: '', cost: '0.00') }

it 'tests parser' do
expect([sample_row]).to eql(parse('/file_test.csv'))

Test outputs -
- were the same, except of instance id - they were obiously different, because those are two different objects, even if they have exactly the same attributes. So I placed
method in my class to control the way Object is presented. Now RSpec shows me two exactly the same
but still test fails. How should I somehow omit the fact two instanes aren't exactly the same if it comes to instance number? I've also tried to use

Answer Source

With all the given informations eq can't work out of the box. You have multiple options:

  • compare every attribute like expect(sample_row.service).to eq(parse('/file_test.csv').first.service)
  • implement Comparable
  • use a third party gem like equalizer to define equality
  • add a method to Call that converts all attributes to a hash and compare those Hashes
  • create your own Matcher
  • ...