rspec-expectations icon indicating copy to clipboard operation
rspec-expectations copied to clipboard

Prevent Aliased Matchers from Overriding Expected Data

Open lnestor opened this issue 6 years ago • 2 comments

Previous Behavior

The aliased matcher implementation of a description currently replaces all instance of the old matcher's name in the new matcher's name. If it happens that the expected values include the old matcher's name, these also get overwritten. In the examples below, the string "include" is replaced in both the outputs by the new matcher's name.

it 'overwrites data for a_string_including' do
  my_string = "a string with 'include'"
  expect("some random string").to match(a_string_including(my_string))
end

# => expected "some random string" to match 
#       (a string including "a string with 'a string including'")
it 'overwrites data for a_hash_including' do
  my_hash = { my_string: "a string with 'include'" }
  expect({}).to match(a_hash_including(my_hash))
end

# => expected {} to match 
#       (a hash including {:my_string => "a string with 'a hash including'"})

New Behavior

This change makes it so it only replaces the first instance of the original name so none of the expected data is also overwritten. The two above error outputs would be the following:

expected {} to match (a hash including {:my_string => "a string with 'include'"}) expected "some random string" to match (a string including "a string with 'include'")

I saw this in https://github.com/rspec/rspec-rails/issues/1835.

lnestor avatar May 24 '19 17:05 lnestor

Do you have any questions on how to proceed with this @lnestor ?

pirj avatar Jan 11 '20 15:01 pirj

@lnestor Are you interested in wrapping this up?

pirj avatar Jun 11 '20 08:06 pirj

Migrated to the monorepo as rspec/rspec#123

JonRowe avatar Nov 30 '24 11:11 JonRowe