Skip to content

Allow use of composable matchers inside be_a_new#with #1811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/rspec/rails/matchers/be_a_new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ def failure_message
message << "expected #{actual.inspect} to be a new #{expected.inspect}"
end
unless attributes_match?(actual)
describe_unmatched_attributes = surface_descriptions_in(unmatched_attributes)
if unmatched_attributes.size > 1
message << "attributes #{unmatched_attributes.inspect} were not set on #{actual.inspect}"
message << "attributes #{describe_unmatched_attributes.inspect} were not set on #{actual.inspect}"
else
message << "attribute #{unmatched_attributes.inspect} was not set on #{actual.inspect}"
message << "attribute #{describe_unmatched_attributes.inspect} was not set on #{actual.inspect}"
end
end
end.join(' and ')
Expand All @@ -49,13 +50,13 @@ def attributes

def attributes_match?(actual)
attributes.stringify_keys.all? do |key, value|
actual.attributes[key].eql?(value)
values_match?(value, actual.attributes[key])
end
end

def unmatched_attributes
attributes.stringify_keys.reject do |key, value|
actual.attributes[key].eql?(value)
values_match?(value, actual.attributes[key])
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions spec/rspec/rails/matchers/be_a_new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,55 @@ def new_record?; true; end
end
end

context "with composable matchers" do
context "one attribute is a composable matcher" do
it "passes" do
expect(record).to be_a_new(record.class).with(
:foo => a_string_including("foo"))
end

it "fails" do
expect {
expect(record).to be_a_new(record.class).with(
:foo => a_string_matching("bar"))
}.to raise_error("attribute {\"foo\"=>(a string matching \"bar\")} was not set on #{record.inspect}")
end

context "matcher is wrong type" do
it "fails" do
expect {
expect(record).to be_a_new(record.class).with(
:foo => a_hash_including({:no_foo => "foo"}))
}.to raise_error {|e|
expect(e.message).to eq("no implicit conversion of Hash into String").or eq("can't convert Hash into String")
}
end
end
end

context "two attributes are composable matchers" do
context "both matchers present in actual" do
it "passes" do
expect(record).to be_a_new(record.class).with(
:foo => a_string_matching("foo"),
:bar => a_string_matching("bar")
)
end
end

context "only one matcher present in actual" do
it "fails" do
expect {
expect(record).to be_a_new(record.class).with(
:foo => a_string_matching("foo"),
:bar => a_string_matching("barn")
)
}.to raise_error("attribute {\"bar\"=>(a string matching \"barn\")} was not set on #{record.inspect}")
end
end
end
end

context "no attributes same" do
it "fails" do
expect {
Expand Down