Skip to content

Instantly share code, notes, and snippets.

@jferris
Created November 11, 2015 21:03
Show Gist options
  • Save jferris/3c11146d817867a6bff1 to your computer and use it in GitHub Desktop.
Save jferris/3c11146d817867a6bff1 to your computer and use it in GitHub Desktop.
Use string comparisons for better spec failure messages
describe Person do
describe "#billable" do
it "returns people with a billable role" do
billable = create(:role, billable: true)
unbillable = create(:role, billable: false)
create(:person, name: "BillableOne", role: billable)
create(:person, name: "BillableTwo", role: billable)
create(:person, name: "Unbillable", role: unbillable)
result = Person.billable
expect(result.map(&:name)).to match_array(%w(BillableOne BillableTwo))
end
end
end
# Failures look like:
#
# 1) Person#billable returns people with a billable role
# Failure/Error: expect(result.map(&:name)).to match_array(%w(BillableOne BillableTwo))
# expected collection contained: ["BillableTwo", "BillableOne"]
# actual collection contained: ["BillableTwo", "Unbillable", "BillableOne"]
# the extra elements were: ["Unbillable"]
# # ./spec/models/person_spec.rb:14:in `block (3 levels) in <top (required)>'
describe Person do
describe "#billable" do
it "returns people with a billable role" do
billable = create(:role, billable: true)
unbillable = create(:role, billable: false)
expected_people = [
create(:person, role: billable),
create(:person, role: billable)
]
create(:person, role: unbillable)
result = Person.billable
expect(result).to match_array(expected_people)
end
end
end
# Failures look like:
#
# 1) Person#billable returns people with a billable role
# Failure/Error: expect(result).to match_array(expected_people)
# expected collection contained: [#<Person id: 6, name: "Name 1", role_id: 4, location_id: nil, manager_id: nil, salary: 0>, #<Person id: 7, name: "Name 2", role_id: 4, location_id: nil, manager_id: nil, salary: 0>]
# actual collection contained: [#<Person id: 6, name: "Name 1", role_id: 4, location_id: nil, manager_id: nil, salary: 0>, #<Person id: 7, name: "Name 2", role_id: 4, location_id: nil, manager_id: nil, salary: 0>, #<Person id: 8, name: "Name 3", role_id: 5, location_id: nil, manager_id: nil, salary: 0>]
# the extra elements were: [#<Person id: 8, name: "Name 3", role_id: 5, location_id: nil, manager_id: nil, salary: 0>]
# # ./spec/models/person_spec.rb:16:in `block (3 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment