Skip to content

Commit d6aa300

Browse files
committed
Ensure that failure messages accurately reflect matchers and remove regex matching from expectations
1 parent b5ac89a commit d6aa300

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

lib/rspec/rails/matchers/be_a_new.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ def failure_message
3232
message << "expected #{actual.inspect} to be a new #{expected.inspect}"
3333
end
3434
unless attributes_match?(actual)
35+
describe_unmatched_attributes = surface_descriptions_in(unmatched_attributes)
3536
if unmatched_attributes.size > 1
36-
message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
37+
message << "attributes #{describe_unmatched_attributes.inspect} were not set on #{actual.inspect}"
3738
else
38-
message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
39+
message << "attribute #{describe_unmatched_attributes.inspect} was not set on #{actual.inspect}"
3940
end
4041
end
4142
end.join(' and ')

spec/rspec/rails/matchers/be_a_new_spec.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ def new_record?; true; end
7676
expect {
7777
expect(record).to be_a_new(record.class).with(
7878
:foo => a_string_matching("bar"))
79-
}.to raise_error{|e|
80-
expect(e.message).to match(/attribute \{.*\} was not set on #{Regexp.escape record.inspect}/)
81-
expect(e.message).to match(/@expected="bar"/)
82-
}
79+
}.to raise_error("attribute {\"foo\"=>(a string matching \"bar\")} was not set on #{record.inspect}")
8380
end
81+
8482
context "matcher is wrong type" do
8583
it "fails" do
8684
expect {
@@ -110,10 +108,7 @@ def new_record?; true; end
110108
:foo => a_string_matching("foo"),
111109
:bar => a_string_matching("barn")
112110
)
113-
}.to raise_error {|e|
114-
expect(e.message).to match(/attribute \{.*\} was not set on #{Regexp.escape record.inspect}/)
115-
expect(e.message).to match(/@expected="barn"/)
116-
}
111+
}.to raise_error("attribute {\"bar\"=>(a string matching \"barn\")} was not set on #{record.inspect}")
117112
end
118113
end
119114
end

0 commit comments

Comments
 (0)