Skip to content

Commit 1967a06

Browse files
authored
Merge pull request #1811 from britnia/issue_1801_inconsistent_matcher_behavior
Allow use of composable matchers inside be_a_new#with
2 parents 1dfde72 + d6aa300 commit 1967a06

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

lib/rspec/rails/matchers/be_a_new.rb

Lines changed: 5 additions & 4 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 ')
@@ -49,13 +50,13 @@ def attributes
4950

5051
def attributes_match?(actual)
5152
attributes.stringify_keys.all? do |key, value|
52-
actual.attributes[key].eql?(value)
53+
values_match?(value, actual.attributes[key])
5354
end
5455
end
5556

5657
def unmatched_attributes
5758
attributes.stringify_keys.reject do |key, value|
58-
actual.attributes[key].eql?(value)
59+
values_match?(value, actual.attributes[key])
5960
end
6061
end
6162
end

spec/rspec/rails/matchers/be_a_new_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,55 @@ def new_record?; true; end
6565
end
6666
end
6767

68+
context "with composable matchers" do
69+
context "one attribute is a composable matcher" do
70+
it "passes" do
71+
expect(record).to be_a_new(record.class).with(
72+
:foo => a_string_including("foo"))
73+
end
74+
75+
it "fails" do
76+
expect {
77+
expect(record).to be_a_new(record.class).with(
78+
:foo => a_string_matching("bar"))
79+
}.to raise_error("attribute {\"foo\"=>(a string matching \"bar\")} was not set on #{record.inspect}")
80+
end
81+
82+
context "matcher is wrong type" do
83+
it "fails" do
84+
expect {
85+
expect(record).to be_a_new(record.class).with(
86+
:foo => a_hash_including({:no_foo => "foo"}))
87+
}.to raise_error {|e|
88+
expect(e.message).to eq("no implicit conversion of Hash into String").or eq("can't convert Hash into String")
89+
}
90+
end
91+
end
92+
end
93+
94+
context "two attributes are composable matchers" do
95+
context "both matchers present in actual" do
96+
it "passes" do
97+
expect(record).to be_a_new(record.class).with(
98+
:foo => a_string_matching("foo"),
99+
:bar => a_string_matching("bar")
100+
)
101+
end
102+
end
103+
104+
context "only one matcher present in actual" do
105+
it "fails" do
106+
expect {
107+
expect(record).to be_a_new(record.class).with(
108+
:foo => a_string_matching("foo"),
109+
:bar => a_string_matching("barn")
110+
)
111+
}.to raise_error("attribute {\"bar\"=>(a string matching \"barn\")} was not set on #{record.inspect}")
112+
end
113+
end
114+
end
115+
end
116+
68117
context "no attributes same" do
69118
it "fails" do
70119
expect {

0 commit comments

Comments
 (0)