Created
January 2, 2013 20:40
-
-
Save JacobNinja/4437781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SetupTestCase < Test::Unit::TestCase | |
def self.test(test_name, *setups, &block) | |
test_with_setups = lambda do | |
setup_procs = setups.map {|setup_func_name| respond_to?(setup_func_name) ? method(setup_func_name) : nil }.compact | |
setup_procs.each(&:call) | |
block.bind(self).call | |
end | |
super(test_name, &test_with_setups) | |
end | |
end | |
class ExampleTest < SetupTestCase | |
def setup | |
@foo = "foo" | |
end | |
def change_foo | |
@foo = "bar" | |
end | |
def found_foo | |
Foo.stubs(:find).returns(@foo) | |
end | |
test "uses global setup" do | |
assert_equal "foo", @foo | |
end | |
test "share stubs between tests", :found_foo do | |
assert_equal @foo, Foo.find(1) | |
end | |
test "runs additional setup after global setup", :change_foo do | |
assert_equal "bar", @foo | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment