Search code examples
unit-testingrspecrspec-rails

Is it possible to change Rspec output on objects?


Given the following spec:

describe Person
  describe "scopes and class methods" do
    let!(:sixty_older)     { FactoryGirl.create :person, birthdate: 60.years.ago }

    describe ".senior" do
      subject { Person.senior }
      it { should include(sixty_older) }
    end
  end
end

Its output in CLI using format --documentation:

Person
  scopes and class methods
    .senior
      should include #<Person person_id: 466, gender: "M", birthdate: "1953-02-07 19:44:22", birthdate_estimated: 0, dead: 0, death_date: nil, cause_of_death: nil, creator: 0, date_created: "0002-11-30 00:00:00", changed_by: nil, date_changed: nil, voided: 0, voided_by: nil, date_voided: nil, void_reason: nil, uuid: "key_4">

Is it possible for me to change the #<Person person_id: ....> to something else?

Say, display just person_id, birthdate and uuid?

Also, if it can be customized on a per test basis and not on the class-level change.


Solution

  • You can pass most rspec matchers a custom failure message, like so:

    describe ".senior" do
      subject { Person.senior }
      it { should include(sixty_older), "should include #{person.person_id}:#{person.uuid}" }
    end