hoodwink.d enhanced

Sat Feb 24

Wrap a predicate in an affirmative and a negative assertion

class Test::Unit::TestCase class << self def generate_assertions_for(predicate) # e.g. :user_list_available? # => user_list_available affirmative_assertion = predicate.to_s[/\w+/] # e.g. user_list_available # => user_list_not_available negative_assertion = affirmative_assertion.sub /([^_]+)$/,'not_\1' [ affirmative_assertion, negative_assertion ].each do |assertion_name| assertion_name[0, 0] = 'assert_' end class_eval(<<-EVAL) def #{affirmative_assertion}(*args, &block) assert #{predicate}(*args, &block) end def #{negative_assertion}(*args, &block) assert !#{predicate}(*args, &block) end EVAL end end end # Generate assertions for some predicate private def mail_sent? initial_value = GeneralMailer.deliveries.size yield initial_value < GeneralMailer.deliveries.size end generate_assertions_for :mail_sent? # Use the affirmative or negative assertions def test_creating_user_fails_because_user_is_invalid invalid_attributes = valid_user_attributes.dup assert invalid_attributes.delete(:user_name) assert_record_not_created User do assert_mail_not_sent do assert_error_message_displayed do post :create, :user => invalid_attributes assert_default_template end end end end
I_use_and_heart

Things used and things hearted