diff --git a/lib/shoulda/active_record_helpers.rb b/lib/shoulda/active_record_helpers.rb index 8c015770..e75572ba 100644 --- a/lib/shoulda/active_record_helpers.rb +++ b/lib/shoulda/active_record_helpers.rb @@ -38,6 +38,7 @@ module ThoughtBot # :nodoc: attributes.each do |attribute| should "require #{attribute} to be set" do object = klass.new + object.send("#{attribute}=", nil) assert !object.valid?, "#{klass.name} does not require #{attribute}." assert object.errors.on(attribute), "#{klass.name} does not require #{attribute}." assert_contains(object.errors.on(attribute), message) @@ -443,4 +444,4 @@ module ThoughtBot # :nodoc: include ThoughtBot::Shoulda::Private end end -end \ No newline at end of file +end diff --git a/lib/shoulda/controller_tests/formats/html.rb b/lib/shoulda/controller_tests/formats/html.rb index a5c3c09a..1cc9f727 100644 --- a/lib/shoulda/controller_tests/formats/html.rb +++ b/lib/shoulda/controller_tests/formats/html.rb @@ -122,7 +122,9 @@ module ThoughtBot # :nodoc: end should "destroy record" do - assert_raises(::ActiveRecord::RecordNotFound) { @record.reload } + assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do + @record.reload + end end end end @@ -155,7 +157,7 @@ module ThoughtBot # :nodoc: end should "not have errors on @#{res.object}" do - assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:" + assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end end end @@ -183,7 +185,7 @@ module ThoughtBot # :nodoc: end should "not have errors on @#{res.object}" do - assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:" + assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end end end diff --git a/lib/shoulda/controller_tests/formats/xml.rb b/lib/shoulda/controller_tests/formats/xml.rb index c6cfe5ff..893248ba 100644 --- a/lib/shoulda/controller_tests/formats/xml.rb +++ b/lib/shoulda/controller_tests/formats/xml.rb @@ -91,7 +91,9 @@ module ThoughtBot # :nodoc: end else should "destroy record" do - assert_raises(::ActiveRecord::RecordNotFound) { @record.reload } + assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do + @record.reload + end end end end @@ -117,7 +119,7 @@ module ThoughtBot # :nodoc: should_assign_to res.object should "not have errors on @#{res.object}" do - assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:" + assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:" end end end diff --git a/lib/shoulda/general.rb b/lib/shoulda/general.rb index c7b06c15..a785a319 100644 --- a/lib/shoulda/general.rb +++ b/lib/shoulda/general.rb @@ -67,15 +67,15 @@ module ThoughtBot # :nodoc: # # assert_save User.new(params) def assert_save(obj) - assert obj.save, "Errors: #{obj.errors.full_messages.join('; ')}" + assert obj.save, "Errors: #{pretty_error_messages obj}" obj.reload end # Asserts that the given object is valid # - # assert_save User.new(params) + # assert_valid User.new(params) def assert_valid(obj) - assert obj.valid?, "Errors: #{obj.errors.full_messages.join('; ')}" + assert obj.valid?, "Errors: #{pretty_error_messages obj}" end # Asserts that the block uses ActionMailer to send emails @@ -95,6 +95,10 @@ module ThoughtBot # :nodoc: def assert_does_not_send_email(&blk) assert_sends_email 0, &blk end + + def pretty_error_messages(obj) + obj.errors.map { |a, m| "#{m} (#{obj.send(a).inspect})" } + end end end