Class: FreshTestEnvironmentCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/test_extensions/fresh_test_environment_creator.rb

Overview

useful if you want to try multiple different configurations of the same class and make sure that they all are compatabible in some way (document in your common tests) creates a clean slate by removing constants… Lets you cleanly document both all the commonalities and all the differences between two different behaviors (caused by two different configurations)

Example:

# Common setup
test_environment_creator = FreshTestEnvironmentCreator.new(:classes_to_remove => [MyModel]) do
  def setup
    @object = ClassWithSomeConfiguration.new
  end
  def test_common_stuff
    # These tests should hold true whether we're acting acts_one_way *or* acts_another_way
    ...
  end
end

# The setup for TestWhenActingOneWay
class MyModel < ActiveRecord::Base
  acts_one_way
end
# The TestWhenActingOneWay TestCase itself
test_environment_creator.create(:TestWhenActingOneWay) do
  def test_1
    ...
  end
end.run!

# The setup for TestWhenActingAnotherWay
class MyModel < ActiveRecord::Base
  acts_another_way
end
# The TestWhenActingAnotherWay TestCase itself
test_environment_creator.create(:TestWhenActingAnotherWay) do
  def test_1
    ...
  end
end.run!

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &common_tests) ⇒ FreshTestEnvironmentCreator

Returns a new instance of FreshTestEnvironmentCreator.



59
60
61
62
# File 'lib/test_extensions/fresh_test_environment_creator.rb', line 59

def initialize(options = {}, &common_tests)
  @classes_to_remove = options[:classes_to_remove] || []
  @common_tests = Module.create(:CommonTests, &common_tests) if block_given?
end

Instance Method Details

#create(test_case_name, &contents_of_class) ⇒ Object



63
64
65
66
67
68
# File 'lib/test_extensions/fresh_test_environment_creator.rb', line 63

def create(test_case_name, &contents_of_class)
  returning test_case_class = Class.create(test_case_name, :superclass => Test::Unit::TestCase, &contents_of_class) do
    test_case_class.send :include, @common_tests if @common_tests
    test_case_class.ignore_access.class_variable_set(:@@classes_to_remove, @classes_to_remove)
  end
end