'Custom RSpec argument matcher spec output

I am trying to get a good output for a custom matcher I wrote:

RSpec::Matchers.define :scope_limited_to do |expected|
  match do |actual|
    @actual = actual.limit_value
    expected == @actual
  end

  failure_message do |actual|
    "expected scope to be limited to #{expected}, but was limited to #{actual} instead"
  end
end

Things work perfectly when using regular expectation:

expect(scope).to scope_limited_to(10)

# output
     Failure/Error: expect(scope).to scope_limited_to(10)
       expected scope to be limited to 10, but was limited to 11 instead

But when I use this matcher as an argument matcher:

expect(PlayerCsv).to receive(:new).with(scope_limited_to(10))

# output
     Failure/Error: PlayerCsv.new(players).perform

       #<PlayerCsv (class)> received :new with unexpected arguments
         expected: (scope limited to 10)
              got: (#<ActiveRecord::Relation [#<Player id: 136, username: "user1", birthdate: "2001-05-13", email: "email..._id: nil, player_merged_to_id: nil, inventory_access_toggled: nil, seon_registration_session: nil>]>)

Is there any way to improve the "got" part of the spec output? Ideally I'd like to see "(scope limited to 11)".



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source