From f19adeaf6a2bb3e0777bc7ff51848b087aa33843 Mon Sep 17 00:00:00 2001 From: pivotal Date: Mon, 5 May 2008 11:47:50 -0700 Subject: [PATCH] nw/ch - Assert_not_valid --- .../assertions/model_assertions.rb | 17 ++++++++ .../test/controller/action_pack_assertions_test.rb | 41 ++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_controller/assertions/model_assertions.rb b/actionpack/lib/action_controller/assertions/model_assertions.rb index 0b43130..d1d0d62 100644 --- a/actionpack/lib/action_controller/assertions/model_assertions.rb +++ b/actionpack/lib/action_controller/assertions/model_assertions.rb @@ -14,6 +14,23 @@ module ActionController assert record.valid?, record.errors.full_messages.join("\n") end end + + + # Asserts that the passed record is NOT valid by ActiveRecord standards, with a particular set of error messages. + # + # ==== Examples + # + # # assert that the name cannot be blank + # model = Model.new(:name => "") + # assert_not_valid(model, "Name cannot be blank") + # + def assert_not_valid(model, *messages) + clean_backtrace do + messages.flatten! + assert !model.valid?, "Found valid active record: expected errors [#{messages.join(',')}] but got #{model.errors.full_messages.sort.join(',')}" + assert_equal messages.sort, model.errors.full_messages.sort + end + end end end end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 1db0575..017fdfc 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -105,6 +105,9 @@ class ActionPackAssertionsController < ActionController::Base def get_invalid_record @record = Class.new do + def initialize(expected_error) + @expected_error = expected_error + end def valid? false @@ -112,10 +115,11 @@ class ActionPackAssertionsController < ActionController::Base def errors Class.new do - def full_messages; ['...stuff...']; end - end.new + def initialize(expected_error); @expected_error = expected_error; end + def full_messages; [@expected_error]; end + end.new(@expected_error) end - end.new + end.new(params[:error] || '...stuff...') render :nothing => true end @@ -466,14 +470,43 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase end def test_assert_valid_failing - get :get_invalid_record + get :get_invalid_record, :error => "...stuff..." begin assert_valid assigns('record') assert false rescue Test::Unit::AssertionFailedError => e + assert e.message.include?("...stuff...") end end + + def test_assert_not_valid + get :get_invalid_record, :error => "...stuff..." + assert_not_valid assigns('record'), '...stuff...' + end + + def test_assert_not_valid_failing_because_record_is_valid + get :get_valid_record + + begin + assert_not_valid assigns('record'), '...stuff...' + assert false + rescue Test::Unit::AssertionFailedError => e + assert e.message.include?("Found valid active record: expected errors [...stuff...] but got .") + end + end + + def test_assert_not_valid_failing_because_of_incorrect_error_messages + get :get_invalid_record, :error => "...stuff..." + + begin + assert_not_valid assigns('record'), '...notstuff...' + assert false + rescue Test::Unit::AssertionFailedError => e + assert e.message.include?("[\"...notstuff...\"]> expected but was\n<[\"...stuff...\"]") + end + end + def test_assert_response_uses_exception_message @controller = AssertResponseWithUnexpectedErrorController.new -- 1.5.4.5 From 8efd9125b9513d4634cbb2ce4197ca548e9dce69 Mon Sep 17 00:00:00 2001 From: pivotal Date: Tue, 6 May 2008 09:32:58 -0700 Subject: [PATCH] nw/ch - Updated assert_not_valid to make params optional --- .../assertions/model_assertions.rb | 11 ++++++++--- .../test/controller/action_pack_assertions_test.rb | 11 +++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_controller/assertions/model_assertions.rb b/actionpack/lib/action_controller/assertions/model_assertions.rb index d1d0d62..256a8e3 100644 --- a/actionpack/lib/action_controller/assertions/model_assertions.rb +++ b/actionpack/lib/action_controller/assertions/model_assertions.rb @@ -16,7 +16,8 @@ module ActionController end - # Asserts that the passed record is NOT valid by ActiveRecord standards, with a particular set of error messages. + # Asserts that the passed record is NOT valid by ActiveRecord standards. If extra parameters are required, asserts + # that the validation error messages exactly match these parameters (which can be provided either as a splat or an array) # # ==== Examples # @@ -27,8 +28,12 @@ module ActionController def assert_not_valid(model, *messages) clean_backtrace do messages.flatten! - assert !model.valid?, "Found valid active record: expected errors [#{messages.join(',')}] but got #{model.errors.full_messages.sort.join(',')}" - assert_equal messages.sort, model.errors.full_messages.sort + if messages.empty? + assert !model.valid?, "Found valid active record" + else + assert !model.valid?, "Found valid active record: expected errors [#{messages.join(',')}] but got #{model.errors.full_messages.sort.join(',')}" + assert_equal messages.sort, model.errors.full_messages.sort + end end end end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 017fdfc..7367b54 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -473,7 +473,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase get :get_invalid_record, :error => "...stuff..." begin - assert_valid assigns('record') + assert_valid assigns("record") assert false rescue Test::Unit::AssertionFailedError => e assert e.message.include?("...stuff...") @@ -482,25 +482,28 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase def test_assert_not_valid get :get_invalid_record, :error => "...stuff..." - assert_not_valid assigns('record'), '...stuff...' + assert_not_valid assigns("record") + assert_not_valid assigns("record"), "...stuff..." end def test_assert_not_valid_failing_because_record_is_valid get :get_valid_record begin - assert_not_valid assigns('record'), '...stuff...' + assert_not_valid assigns("record"), "...stuff..." assert false rescue Test::Unit::AssertionFailedError => e assert e.message.include?("Found valid active record: expected errors [...stuff...] but got .") end + + assert_raises(Test::Unit::AssertionFailedError, "Found valid active record") {assert_not_valid assigns("record")} end def test_assert_not_valid_failing_because_of_incorrect_error_messages get :get_invalid_record, :error => "...stuff..." begin - assert_not_valid assigns('record'), '...notstuff...' + assert_not_valid assigns("record"), "...notstuff..." assert false rescue Test::Unit::AssertionFailedError => e assert e.message.include?("[\"...notstuff...\"]> expected but was\n<[\"...stuff...\"]") -- 1.5.4.5