Permalink
Browse files

Use better assertion methods for testing

[#4645 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent b753b4a commit b4629528866446aa59f663a1162edbdacee85600 Neeraj Singh committed with josevalim May 19, 2010
Showing with 116 additions and 123 deletions.
  1. +2 −2 actionmailer/test/base_test.rb
  2. +4 −4 actionmailer/test/old_base/asset_host_test.rb
  3. +1 −1 actionmailer/test/old_base/mail_render_test.rb
  4. +11 −11 actionmailer/test/old_base/mail_service_test.rb
  5. +2 −2 actionmailer/test/test_helper_test.rb
  6. +7 −7 actionpack/test/controller/filters_test.rb
  7. +2 −2 actionpack/test/controller/new_base/bare_metal_test.rb
  8. +1 −1 activemodel/test/cases/attribute_methods_test.rb
  9. +1 −1 activemodel/test/cases/callbacks_test.rb
  10. +2 −2 activemodel/test/cases/dirty_test.rb
  11. +1 −1 activemodel/test/cases/serializeration/xml_serialization_test.rb
  12. +10 −10 activerecord/test/cases/aggregations_test.rb
  13. +2 −2 activerecord/test/cases/associations/belongs_to_associations_test.rb
  14. +1 −1 activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
  15. +1 −1 activerecord/test/cases/associations/eager_test.rb
  16. +3 −10 activerecord/test/cases/associations/has_many_associations_test.rb
  17. +2 −2 activerecord/test/cases/associations/has_many_through_associations_test.rb
  18. +4 −4 activerecord/test/cases/associations/has_one_associations_test.rb
  19. +9 −9 activerecord/test/cases/associations/inverse_associations_test.rb
  20. +2 −2 activerecord/test/cases/associations/join_model_test.rb
  21. +1 −1 activerecord/test/cases/attribute_methods_test.rb
  22. +4 −4 activerecord/test/cases/autosave_association_test.rb
  23. +25 −25 activerecord/test/cases/base_test.rb
  24. +3 −3 activerecord/test/cases/column_definition_test.rb
  25. +2 −2 activerecord/test/cases/defaults_test.rb
  26. +2 −2 activerecord/test/cases/dirty_test.rb
  27. +11 −11 activerecord/test/cases/finder_respond_to_test.rb
@@ -491,8 +491,8 @@ def give_a_greeting
# Class level API with method missing
test "should respond to action methods" do
- assert BaseMailer.respond_to?(:welcome)
- assert BaseMailer.respond_to?(:implicit_multipart)
+ assert_respond_to BaseMailer, :welcome
+ assert_respond_to BaseMailer, :implicit_multipart
assert !BaseMailer.respond_to?(:mail)
assert !BaseMailer.respond_to?(:headers)
end
@@ -26,7 +26,7 @@ def teardown
def test_asset_host_as_string
mail = AssetHostMailer.email_with_asset
- assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip
+ assert_equal %Q{<img alt="Somelogo" src="http://www.example.com/images/somelogo.png" />}, mail.body.to_s.strip
end
def test_asset_host_as_one_arguement_proc
@@ -38,7 +38,7 @@ def test_asset_host_as_one_arguement_proc
end
}
mail = AssetHostMailer.email_with_asset
- assert_equal "<img alt=\"Somelogo\" src=\"http://images.example.com/images/somelogo.png\" />", mail.body.to_s.strip
+ assert_equal %Q{<img alt="Somelogo" src="http://images.example.com/images/somelogo.png" />}, mail.body.to_s.strip
end
def test_asset_host_as_two_arguement_proc
@@ -51,6 +51,6 @@ def test_asset_host_as_two_arguement_proc
}
mail = nil
assert_nothing_raised { mail = AssetHostMailer.email_with_asset }
- assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip
+ assert_equal %Q{<img alt="Somelogo" src="http://www.example.com/images/somelogo.png" />}, mail.body.to_s.strip
end
-end
+end
@@ -117,7 +117,7 @@ def test_file_template
def test_rxml_template
mail = RenderMailer.rxml_template.deliver
- assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test/>", mail.body.to_s.strip
+ assert_equal %(<?xml version="1.0" encoding="UTF-8"?>\n<test/>), mail.body.to_s.strip
end
def test_included_subtemplate
@@ -281,23 +281,23 @@ def headers_with_nonalpha_chars(recipient)
from "One: Two <test@example.com>"
cc "Three: Four <test@example.com>"
bcc "Five: Six <test@example.com>"
- body "testing"
+ body "testing"
end
def custom_content_type_attributes
recipients "no.one@nowhere.test"
subject "custom content types"
from "some.one@somewhere.test"
content_type "text/plain; format=flowed"
- body "testing"
+ body "testing"
end
def return_path
recipients "no.one@nowhere.test"
subject "return path test"
from "some.one@somewhere.test"
headers["return-path"] = "another@somewhere.test"
- body "testing"
+ body "testing"
end
def subject_with_i18n(recipient)
@@ -417,7 +417,7 @@ def test_signed_up
end
def test_custom_template
- expected = new_mail
+ expected = new_mail
expected.to = @recipient
expected.subject = "[Signed up] Welcome #{@recipient}"
expected.body = "Hello there, \n\nMr. #{@recipient}"
@@ -436,7 +436,7 @@ def test_custom_templating_extension
assert ActionView::Template.template_handler_extensions.include?("haml"), "haml extension was not registered"
# N.b., custom_templating_extension.text.plain.haml is expected to be in fixtures/test_mailer directory
- expected = new_mail
+ expected = new_mail
expected.to = @recipient
expected.subject = "[Signed up] Welcome #{@recipient}"
expected.body = "Hello there, \n\nMr. #{@recipient}"
@@ -453,7 +453,7 @@ def test_custom_templating_extension
end
def test_cancelled_account
- expected = new_mail
+ expected = new_mail
expected.to = @recipient
expected.subject = "[Cancelled] Goodbye #{@recipient}"
expected.body = "Goodbye, Mr. #{@recipient}"
@@ -477,7 +477,7 @@ def test_cancelled_account
end
def test_cc_bcc
- expected = new_mail
+ expected = new_mail
expected.to = @recipient
expected.subject = "testing bcc/cc"
expected.body = "Nothing to see here."
@@ -602,7 +602,7 @@ def test_iso_charset
def test_unencoded_subject
TestMailer.delivery_method = :test
- expected = new_mail
+ expected = new_mail
expected.to = @recipient
expected.subject = "testing unencoded subject"
expected.body = "Nothing to see here."
@@ -1151,15 +1151,15 @@ def teardown
end
def test_should_respond_to_new
- assert RespondToMailer.respond_to?(:new)
+ assert_respond_to RespondToMailer, :new
end
def test_should_respond_to_create_with_template_suffix
- assert RespondToMailer.respond_to?(:create_any_old_template)
+ assert_respond_to RespondToMailer, :create_any_old_template
end
def test_should_respond_to_deliver_with_template_suffix
- assert RespondToMailer.respond_to?(:deliver_any_old_template)
+ assert_respond_to RespondToMailer, :deliver_any_old_template
end
def test_should_not_respond_to_new_with_template_suffix
@@ -18,7 +18,7 @@ def test_setup_sets_right_action_mailer_options
end
def test_setup_creates_the_expected_mailer
- assert @expected.is_a?(Mail::Message)
+ assert_kind_of Mail::Message, @expected
assert_equal "1.0", @expected.mime_version
assert_equal "text/plain", @expected.mime_type
end
@@ -121,7 +121,7 @@ def setup
end
def test_setup_shouldnt_conflict_with_mailer_setup
- assert @expected.is_a?(Mail::Message)
+ assert_kind_of Mail::Message, @expected
assert_equal 'a value', @test_var
end
end
@@ -515,7 +515,7 @@ def test_running_anomolous_yet_valid_condition_filters
assert assigns["ran_proc_filter2"]
test_process(AnomolousYetValidConditionController, "show_without_filter")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
assert !assigns["ran_class_filter"]
assert !assigns["ran_proc_filter1"]
assert !assigns["ran_proc_filter2"]
@@ -530,16 +530,16 @@ def test_running_collection_condition_filters
test_process(ConditionalCollectionFilterController)
assert_equal %w( ensure_login ), assigns["ran_filter"]
test_process(ConditionalCollectionFilterController, "show_without_filter")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
test_process(ConditionalCollectionFilterController, "another_action")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
end
def test_running_only_condition_filters
test_process(OnlyConditionSymController)
assert_equal %w( ensure_login ), assigns["ran_filter"]
test_process(OnlyConditionSymController, "show_without_filter")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
test_process(OnlyConditionProcController)
assert assigns["ran_proc_filter"]
@@ -556,7 +556,7 @@ def test_running_except_condition_filters
test_process(ExceptConditionSymController)
assert_equal %w( ensure_login ), assigns["ran_filter"]
test_process(ExceptConditionSymController, "show_without_filter")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
test_process(ExceptConditionProcController)
assert assigns["ran_proc_filter"]
@@ -573,7 +573,7 @@ def test_running_before_and_after_condition_filters
test_process(BeforeAndAfterConditionController)
assert_equal %w( ensure_login clean_up_tmp), assigns["ran_filter"]
test_process(BeforeAndAfterConditionController, "show_without_filter")
- assert_equal nil, assigns["ran_filter"]
+ assert_nil assigns["ran_filter"]
end
def test_around_filter
@@ -674,7 +674,7 @@ def test_condition_skipping_of_filters_when_siblings_also_have_conditions
def test_changing_the_requirements
test_process(ChangingTheRequirementsController, "go_wild")
- assert_equal nil, assigns['ran_filter']
+ assert_nil assigns['ran_filter']
end
def test_a_rescuing_around_filter
@@ -18,10 +18,10 @@ class BareTest < ActiveSupport::TestCase
string << part
end
- assert headers.is_a?(Hash), "Headers must be a Hash"
+ assert_kind_of Hash, headers, "Headers must be a Hash"
assert headers["Content-Type"], "Content-Type must exist"
assert_equal "Hello world", string
end
end
-end
+end
@@ -31,7 +31,7 @@ class AttributeMethodsTest < ActiveModel::TestCase
ModelWithAttributes.define_attribute_methods([:foo])
assert ModelWithAttributes.attribute_methods_generated?
- assert ModelWithAttributes.new.respond_to?(:foo)
+ assert_respond_to ModelWithAttributes.new, :foo
assert_equal "value of foo", ModelWithAttributes.new.foo
end
@@ -65,6 +65,6 @@ def create
test "only selects which types of callbacks should be created" do
assert !ModelCallbacks.respond_to?(:before_initialize)
assert !ModelCallbacks.respond_to?(:around_initialize)
- assert ModelCallbacks.respond_to?(:after_initialize)
+ assert_respond_to ModelCallbacks, :after_initialize
end
end
@@ -22,8 +22,8 @@ def name=(val)
test "changes accessible through both strings and symbols" do
model = DirtyModel.new
model.name = "David"
- assert !model.changes[:name].nil?
- assert !model.changes['name'].nil?
+ assert_not_nil model.changes[:name]
+ assert_not_nil model.changes['name']
end
end
@@ -69,7 +69,7 @@ def setup
test "should allow skipped types" do
@xml = @contact.to_xml :skip_types => true
- assert %r{<age>25</age>}.match(@xml)
+ assert_match %r{<age>25</age>}, @xml
end
test "should include yielded additions" do
@@ -58,36 +58,36 @@ def test_reloaded_instance_refreshes_aggregations
end
def test_gps_equality
- assert GpsLocation.new('39x110') == GpsLocation.new('39x110')
+ assert_equal GpsLocation.new('39x110'), GpsLocation.new('39x110')
end
def test_gps_inequality
- assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
+ assert_not_equal GpsLocation.new('39x110'), GpsLocation.new('39x111')
end
def test_allow_nil_gps_is_nil
- assert_equal nil, customers(:zaphod).gps_location
+ assert_nil customers(:zaphod).gps_location
end
def test_allow_nil_gps_set_to_nil
customers(:david).gps_location = nil
customers(:david).save
customers(:david).reload
- assert_equal nil, customers(:david).gps_location
+ assert_nil customers(:david).gps_location
end
def test_allow_nil_set_address_attributes_to_nil
customers(:zaphod).address = nil
- assert_equal nil, customers(:zaphod).attributes[:address_street]
- assert_equal nil, customers(:zaphod).attributes[:address_city]
- assert_equal nil, customers(:zaphod).attributes[:address_country]
+ assert_nil customers(:zaphod).attributes[:address_street]
+ assert_nil customers(:zaphod).attributes[:address_city]
+ assert_nil customers(:zaphod).attributes[:address_country]
end
def test_allow_nil_address_set_to_nil
customers(:zaphod).address = nil
customers(:zaphod).save
customers(:zaphod).reload
- assert_equal nil, customers(:zaphod).address
+ assert_nil customers(:zaphod).address
end
def test_nil_raises_error_when_allow_nil_is_false
@@ -104,9 +104,9 @@ def test_allow_nil_address_loaded_when_only_some_attributes_are_nil
def test_nil_assignment_results_in_nil
customers(:david).gps_location = GpsLocation.new('39x111')
- assert_not_equal nil, customers(:david).gps_location
+ assert_not_nil customers(:david).gps_location
customers(:david).gps_location = nil
- assert_equal nil, customers(:david).gps_location
+ assert_nil customers(:david).gps_location
end
def test_custom_constructor
@@ -403,7 +403,7 @@ def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_recor
assert_equal saved_member.id, sponsor.sponsorable_id
sponsor.sponsorable = new_member
- assert_equal nil, sponsor.sponsorable_id
+ assert_nil sponsor.sponsorable_id
end
def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
@@ -415,7 +415,7 @@ def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_ne
assert_equal saved_writer.name, essay.writer_id
essay.writer = new_writer
- assert_equal nil, essay.writer_id
+ assert_nil essay.writer_id
end
def test_belongs_to_proxy_should_not_respond_to_private_methods
@@ -29,7 +29,7 @@ def test_class_names
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging )
- assert_equal 'Tagging', post.tagging.class.name
+ assert_instance_of Tagging, post.tagging
ensure
ActiveRecord::Base.store_full_sti_class = old
end
@@ -110,7 +110,7 @@ def test_finding_with_includes_on_has_many_association_with_same_include_include
author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
author.posts_with_comments.each do |post_with_comments|
assert_equal post_with_comments.comments.length, post_with_comments.comments.count
- assert_equal nil, post_with_comments.comments.uniq!
+ assert_nil post_with_comments.comments.uniq!
end
end
@@ -175,14 +175,7 @@ def test_belongs_to_sanity
c = Client.new
assert_nil c.firm
- if c.firm
- assert false, "belongs_to failed if check"
- end
-
- unless c.firm
- else
- assert false, "belongs_to failed unless check"
- end
+ flunk "belongs_to failed if check" if c.firm
end
def test_find_ids
@@ -620,7 +613,7 @@ def test_clearing_an_exclusively_dependent_association_collection
assert_equal [], Client.destroyed_client_ids[firm.id]
# Should be destroyed since the association is exclusively dependent.
- assert Client.find_by_id(client_id).nil?
+ assert_nil Client.find_by_id(client_id)
end
def test_dependent_association_respects_optional_conditions_on_delete
@@ -669,7 +662,7 @@ def test_delete_all_association_with_primary_key_deletes_correct_records
old_record = firm.clients_using_primary_key_with_delete_all.first
firm = Firm.find(:first)
firm.destroy
- assert Client.find_by_id(old_record.id).nil?
+ assert_nil Client.find_by_id(old_record.id)
end
def test_creation_respects_hash_condition
Oops, something went wrong.

0 comments on commit b462952

Please sign in to comment.