Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code gardening: we have assert_(nil|blank|present), more concise, wit…

…h better default failure messages - let's use them
  • Loading branch information...
commit fb6b80562041e8d2378cad1b51f8c234fe76fd5e 1 parent d14e298
Xavier Noria fxn authored
2  actionpack/test/controller/output_escaping_test.rb
View
@@ -3,7 +3,7 @@
class OutputEscapingTest < ActiveSupport::TestCase
test "escape_html shouldn't die when passed nil" do
- assert ERB::Util.h(nil).blank?
+ assert_blank ERB::Util.h(nil)
end
test "escapeHTML should escape strings" do
12 actionpack/test/controller/render_test.rb
View
@@ -1113,7 +1113,7 @@ def test_overwritting_rendering_relative_file_with_extension
def test_head_with_location_header
get :head_with_location_header
- assert @response.body.blank?
+ assert_blank @response.body
assert_equal "/foo", @response.headers["Location"]
assert_response :ok
end
@@ -1126,7 +1126,7 @@ def test_head_with_location_object
end
get :head_with_location_object
- assert @response.body.blank?
+ assert_blank @response.body
assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
assert_response :ok
end
@@ -1134,7 +1134,7 @@ def test_head_with_location_object
def test_head_with_custom_header
get :head_with_custom_header
- assert @response.body.blank?
+ assert_blank @response.body
assert_equal "something", @response.headers["X-Custom-Header"]
assert_response :ok
end
@@ -1414,7 +1414,7 @@ def test_render_should_not_set_etag_when_last_modified_has_been_specified
assert_equal 200, @response.status.to_i
assert_not_nil @response.last_modified
assert_nil @response.etag
- assert @response.body.present?
+ assert_present @response.body
end
def test_render_with_etag
@@ -1499,7 +1499,7 @@ def test_request_not_modified
@request.if_modified_since = @last_modified
get :conditional_hello
assert_equal 304, @response.status.to_i
- assert @response.body.blank?, @response.body
+ assert_blank @response.body
assert_equal @last_modified, @response.headers['Last-Modified']
end
@@ -1514,7 +1514,7 @@ def test_request_modified
@request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
get :conditional_hello
assert_equal 200, @response.status.to_i
- assert !@response.body.blank?
+ assert_present @response.body
assert_equal @last_modified, @response.headers['Last-Modified']
end
2  actionpack/test/controller/request_forgery_protection_test.rb
View
@@ -251,7 +251,7 @@ def test_should_allow_all_methods_without_token
test 'should not emit a csrf-token meta tag' do
get :meta
- assert @response.body.blank?
+ assert_blank @response.body
end
end
2  actionpack/test/controller/test_test.rb
View
@@ -546,7 +546,7 @@ def test_params_reset_after_post_request
assert_equal "bar", @request.params[:foo]
@request.recycle!
post :no_op
- assert @request.params[:foo].blank?
+ assert_blank @request.params[:foo]
end
def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set
4 actionpack/test/controller/webservice_test.rb
View
@@ -29,7 +29,7 @@ def setup
def test_check_parameters
with_test_route_set do
get "/"
- assert @controller.response.body.blank?
+ assert_blank @controller.response.body
end
end
@@ -161,7 +161,7 @@ def test_register_and_use_xml_simple
def test_use_xml_ximple_with_empty_request
with_test_route_set do
assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
- assert @controller.response.body.blank?
+ assert_blank @controller.response.body
end
end
2  actionpack/test/template/atom_feed_helper_test.rb
View
@@ -226,7 +226,7 @@ def test_providing_builder_to_atom_feed
get :index, :id=>"provide_builder"
# because we pass in the non-default builder, the content generated by the
# helper should go 'nowhere'. Leaving the response body blank.
- assert @response.body.blank?
+ assert_blank @response.body
end
end
6 activemodel/test/cases/mass_assignment_security_test.rb
View
@@ -25,13 +25,13 @@ def test_attributes_protected_by_default
end
def test_mass_assignment_protection_inheritance
- assert LoosePerson.accessible_attributes.blank?
+ assert_blank LoosePerson.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator']), LoosePerson.protected_attributes
- assert LooseDescendant.accessible_attributes.blank?
+ assert_blank LooseDescendant.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes
- assert LooseDescendantSecond.accessible_attributes.blank?
+ assert_blank LooseDescendantSecond.accessible_attributes
assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes,
'Running attr_protected twice in one class should merge the protections'
2  activerecord/test/cases/aggregations_test.rb
View
@@ -99,7 +99,7 @@ def test_allow_nil_address_loaded_when_only_some_attributes_are_nil
customers(:zaphod).save
customers(:zaphod).reload
assert_kind_of Address, customers(:zaphod).address
- assert customers(:zaphod).address.street.nil?
+ assert_nil customers(:zaphod).address.street
end
def test_nil_assignment_results_in_nil
2  activerecord/test/cases/associations/belongs_to_associations_test.rb
View
@@ -22,7 +22,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
def test_belongs_to
Client.find(3).firm.name
assert_equal companies(:first_firm).name, Client.find(3).firm.name
- assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
+ assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
end
def test_belongs_to_with_primary_key
4 activerecord/test/cases/associations/has_many_associations_test.rb
View
@@ -633,7 +633,7 @@ def test_clearing_an_association_collection
# Should not be destroyed since the association is not dependent.
assert_nothing_raised do
- assert Client.find(client_id).firm.nil?
+ assert_nil Client.find(client_id).firm
end
end
@@ -658,7 +658,7 @@ def test_clearing_a_dependent_association_collection
assert_equal [client_id], Client.destroyed_client_ids[firm.id]
# Should be destroyed since the association is dependent.
- assert Client.find_by_id(client_id).nil?
+ assert_nil Client.find_by_id(client_id)
end
def test_clearing_an_exclusively_dependent_association_collection
2  activerecord/test/cases/associations/has_one_associations_test.rb
View
@@ -253,7 +253,7 @@ def test_dependence_with_missing_association
def test_dependence_with_missing_association_and_nullify
Account.destroy_all
firm = DependentFirm.find(:first)
- assert firm.account.nil?
+ assert_nil firm.account
firm.destroy
end
4 activerecord/test/cases/associations/join_model_test.rb
View
@@ -427,8 +427,8 @@ def test_add_to_self_referential_has_many_through
def test_has_many_through_uses_conditions_specified_on_the_has_many_association
author = Author.find(:first)
- assert !author.comments.blank?
- assert author.nonexistant_comments.blank?
+ assert_present author.comments
+ assert_blank author.nonexistant_comments
end
def test_has_many_through_uses_correct_attributes
2  activerecord/test/cases/method_scoping_test.rb
View
@@ -449,7 +449,7 @@ def test_nested_exclusive_scope_for_create
Comment.send(:with_scope, :create => { :body => "Hey guys, nested scopes are broken. Please fix!" }) do
Comment.send(:with_exclusive_scope, :create => { :post_id => 1 }) do
assert_equal({:post_id => 1}, Comment.scoped.send(:scope_for_create))
- assert Comment.new.body.blank?
+ assert_blank Comment.new.body
comment = Comment.create :body => "Hey guys"
end
end
4 activerecord/test/cases/named_scope_test.rb
View
@@ -480,8 +480,8 @@ def test_dynamic_scope
end
def test_dynamic_scope_should_create_methods_after_hitting_method_missing
- assert Developer.methods.grep(/scoped_by_created_at/).blank?
+ assert_blank Developer.methods.grep(/scoped_by_created_at/)
Developer.scoped_by_created_at(nil)
- assert Developer.methods.grep(/scoped_by_created_at/).present?
+ assert_present Developer.methods.grep(/scoped_by_created_at/)
end
end
2  activerecord/test/cases/primary_keys_test.rb
View
@@ -11,7 +11,7 @@ class PrimaryKeysTest < ActiveRecord::TestCase
def test_to_key_with_default_primary_key
topic = Topic.new
- assert topic.to_key.nil?
+ assert_nil topic.to_key
topic = Topic.find(1)
assert_equal [1], topic.to_key
end
2  activerecord/test/cases/relation_scoping_test.rb
View
@@ -209,7 +209,7 @@ def test_nested_scoped_create
def test_nested_exclusive_scope_for_create
comment = Comment.create_with(:body => "Hey guys, nested scopes are broken. Please fix!").scoping do
Comment.unscoped.create_with(:post_id => 1).scoping do
- assert Comment.new.body.blank?
+ assert_blank Comment.new.body
Comment.create :body => "Hey guys"
end
end
4 activerecord/test/cases/relations_test.rb
View
@@ -411,7 +411,7 @@ def test_find_ids
def test_find_in_empty_array
authors = Author.scoped.where(:id => [])
- assert authors.all.blank?
+ assert_blank authors.all
end
def test_exists
@@ -486,7 +486,7 @@ def test_relation_merging_with_eager_load
def test_relation_merging_with_locks
devs = Developer.lock.where("salary >= 80000").order("id DESC") & Developer.limit(2)
- assert devs.locked.present?
+ assert_present devs.locked
end
def test_relation_merging_with_preload
10 activesupport/lib/active_support/testing/assertions.rb
View
@@ -67,15 +67,17 @@ def assert_no_difference(expression, message = nil, &block)
# Test if an expression is blank. Passes if object.blank? is true.
#
# assert_blank [] # => true
- def assert_blank(object)
- assert object.blank?, "#{object.inspect} is not blank"
+ def assert_blank(object, message=nil)
+ message ||= "#{object.inspect} is not blank"
+ assert object.blank?, message
end
# Test if an expression is not blank. Passes if object.present? is true.
#
# assert_present {:data => 'x' } # => true
- def assert_present(object)
- assert object.present?, "#{object.inspect} is blank"
+ def assert_present(object, message=nil)
+ message ||= "#{object.inspect} is blank"
+ assert object.present?, message
end
end
end
4 activesupport/test/caching_test.rb
View
@@ -649,12 +649,12 @@ def setup
def test_logging
@cache.fetch('foo') { 'bar' }
- assert @buffer.string.present?
+ assert_present @buffer.string
end
def test_mute_logging
@cache.mute { @cache.fetch('foo') { 'bar' } }
- assert @buffer.string.blank?
+ assert_blank @buffer.string
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.