Created
August 6, 2009 08:43
-
-
Save stefanoc/163199 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 ActiveSupport::TestCase | |
class << self | |
def context(name, &block) | |
_context_stack_.unshift name.gsub(/\s+/,'_') | |
define_callbacks :"setup_#{_current_context_}", :"teardown_#{_current_context_}" | |
class_eval(&block) | |
_context_stack_.pop | |
end | |
def test_with_context(name, &block) | |
if _current_context_ | |
callbacks = [:"setup_#{_current_context_}", :"teardown_#{_current_context_}"] | |
test_without_context("#{_current_context_}__#{name}") do | |
run_callbacks callbacks[0] | |
block.bind(self).call | |
run_callbacks callbacks[1] | |
end | |
else | |
test_without_context(name, &block) | |
end | |
end | |
def setup_with_context(*methods, &block) | |
if _current_context_ | |
self.send("setup_#{_current_context_}", *methods, &block) | |
else | |
setup_without_context(*methods, &block) | |
end | |
end | |
def teardown_with_context(*methods, &block) | |
if _current_context_ | |
self.send("teardown_#{_current_context_}", *methods, &block) | |
else | |
teardown_without_context(*methods, &block) | |
end | |
end | |
def _context_stack_ | |
@_context_stack_ ||= [] | |
end | |
def _current_context_ | |
_context_stack_[0] | |
end | |
alias_method_chain :test, :context | |
alias_method_chain :setup, :context | |
alias_method_chain :teardown, :context | |
end | |
end | |
class MyTest < ActiveSupport::TestCase | |
setup :common_setup | |
context "When logged in" do | |
setup :login | |
test "GET /" do | |
end | |
end | |
context "When logged out" do | |
test "GET /" do | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment