code gardening: we have assert_(nil|blank|present), more concise, with better default failure messages - let's use them

This commit is contained in:
Xavier Noria 2010-08-17 03:29:57 +02:00
parent d14e2987b5
commit fb6b805620
19 changed files with 37 additions and 35 deletions

View File

@ -3,7 +3,7 @@ require 'abstract_unit'
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

View File

@ -1113,7 +1113,7 @@ class RenderTest < ActionController::TestCase
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 @@ class RenderTest < ActionController::TestCase
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 @@ class RenderTest < ActionController::TestCase
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 @@ class EtagRenderTest < ActionController::TestCase
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 @@ class LastModifiedRenderTest < ActionController::TestCase
@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 @@ class LastModifiedRenderTest < ActionController::TestCase
@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

View File

@ -251,7 +251,7 @@ class FreeCookieControllerTest < ActionController::TestCase
test 'should not emit a csrf-token meta tag' do
get :meta
assert @response.body.blank?
assert_blank @response.body
end
end

View File

@ -546,7 +546,7 @@ XML
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

View File

@ -29,7 +29,7 @@ class WebServiceTest < ActionController::IntegrationTest
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 @@ class WebServiceTest < ActionController::IntegrationTest
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

View File

@ -226,7 +226,7 @@ class AtomFeedTest < ActionController::TestCase
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

View File

@ -25,13 +25,13 @@ class MassAssignmentSecurityTest < ActiveModel::TestCase
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'

View File

@ -99,7 +99,7 @@ class AggregationsTest < ActiveRecord::TestCase
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

View File

@ -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

View File

@ -633,7 +633,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
# 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 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
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

View File

@ -253,7 +253,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
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

View File

@ -427,8 +427,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
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

View File

@ -449,7 +449,7 @@ class NestedScopingTest < ActiveRecord::TestCase
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

View File

@ -480,8 +480,8 @@ class DynamicScopeTest < ActiveRecord::TestCase
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

View File

@ -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

View File

@ -209,7 +209,7 @@ class NestedRelationScopingTest < ActiveRecord::TestCase
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

View File

@ -411,7 +411,7 @@ class RelationTest < ActiveRecord::TestCase
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 @@ class RelationTest < ActiveRecord::TestCase
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

View File

@ -67,15 +67,17 @@ module ActiveSupport
# 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

View File

@ -649,12 +649,12 @@ class CacheStoreLoggerTest < ActiveSupport::TestCase
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