Skip to content
Browse files

added assert_valid to AP

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2635 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 5e5c332 commit 0279949b055330209eb272ed642e1fe59536087d Tobias Lütke committed Oct 16, 2005
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added assert_vaild. Reports the proper AR error messages as fail message when the passed record is invalid [Tobias Luetke]
+
* Add temporary support for passing locals to render using string keys [Nicholas Seckar]
* Clean up error pages by providing better backtraces [Nicholas Seckar]
View
7 actionpack/lib/action_controller/assertions.rb
@@ -283,6 +283,13 @@ def assert_dom_not_equal(expected, actual, message="")
assert_block(full_message) { expected_dom != actual_dom }
end
end
+
+ # ensures that the passed record is valid by active record standards. returns the error messages if not
+ def assert_valid(record)
+ clean_backtrace do
+ assert record.valid?, record.errors.full_messages
+ end
+ end
def clean_backtrace(&block)
begin
View
52 actionpack/test/controller/action_pack_assertions_test.rb
@@ -77,6 +77,41 @@ def raise_on_get
def raise_on_post
raise "post" if @request.post?
render_text "request method: #{@request.env['REQUEST_METHOD']}"
+ end
+
+ def get_valid_record
+ @record = Class.new do
+ def valid?
+ true
+ end
+
+ def errors
+ Class.new do
+ def full_messages; '...stuff...'; end
+ end.new
+ end
+
+ end.new
+
+ render :nothing => true
+ end
+
+
+ def get_invalid_record
+ @record = Class.new do
+
+ def valid?
+ false
+ end
+
+ def errors
+ Class.new do
+ def full_messages; '...stuff...'; end
+ end.new
+ end
+ end.new
+
+ render :nothing => true
end
# 911
@@ -420,6 +455,21 @@ def test_redirected_to_with_nested_controller
get :redirect_to_fellow_controller
assert_redirected_to :controller => 'admin/user'
+ end
+
+ def test_assert_valid
+ get :get_valid_record
+ assert_valid assigns('record')
+ end
+
+ def test_assert_valid_failing
+ get :get_invalid_record
+
+ begin
+ assert_valid assigns('record')
+ assert false
+ rescue Test::Unit::AssertionFailedError => e
+ end
end
end
@@ -437,4 +487,4 @@ def test_rendering_xml_respects_content_type
process :hello_xml_world
assert_equal('application/pdf', @controller.headers['Content-Type'])
end
-end
+end
View
3 actionpack/test/controller/active_record_assertions_test.rb
@@ -75,8 +75,7 @@ def bad_company
# the safety dance......
def rescue_action(e) raise; end
end
-
-
+
class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase
def setup
@request = ActionController::TestRequest.new

0 comments on commit 0279949

Please sign in to comment.
Something went wrong with that request. Please try again.