Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activesupport/lib/active_support/testing/setup_and_teardown.rb

Revision 8664, 2.6 kB (checked in by bitsweat, 8 months ago)

Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. Closes #10727.

Line 
1 module ActiveSupport
2   module Testing
3     module SetupAndTeardown
4       def self.included(base)
5         base.send :include, ActiveSupport::Callbacks
6         base.define_callbacks :setup, :teardown
7
8         begin
9           require 'mocha'
10           base.alias_method_chain :run, :callbacks_and_mocha
11         rescue LoadError
12           base.alias_method_chain :run, :callbacks
13         end
14       end
15
16       # This redefinition is unfortunate but test/unit shows us no alternative.
17       def run_with_callbacks(result) #:nodoc:
18         return if @method_name.to_s == "default_test"
19
20         yield(Test::Unit::TestCase::STARTED, name)
21         @_result = result
22         begin
23           run_callbacks :setup
24           setup
25           __send__(@method_name)
26         rescue Test::Unit::AssertionFailedError => e
27           add_failure(e.message, e.backtrace)
28         rescue *Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
29           raise
30         rescue Exception
31           add_error($!)
32         ensure
33           begin
34             teardown
35             run_callbacks :teardown, :enumerator => :reverse_each
36           rescue Test::Unit::AssertionFailedError => e
37             add_failure(e.message, e.backtrace)
38           rescue *Test::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
39             raise
40           rescue Exception
41             add_error($!)
42           end
43         end
44         result.add_run
45         yield(Test::Unit::TestCase::FINISHED, name)
46       end
47
48       # Doubly unfortunate: mocha does the same so we have to hax their hax.
49       def run_with_callbacks_and_mocha(result)
50         return if @method_name.to_s == "default_test"
51
52         yield(Test::Unit::TestCase::STARTED, name)
53         @_result = result
54         begin
55           mocha_setup
56           begin
57             run_callbacks :setup
58             setup
59             __send__(@method_name)
60             mocha_verify { add_assertion }
61           rescue Mocha::ExpectationError => e
62             add_failure(e.message, e.backtrace)
63           rescue Test::Unit::AssertionFailedError => e
64             add_failure(e.message, e.backtrace)
65           rescue StandardError, ScriptError
66             add_error($!)
67           ensure
68             begin
69               teardown
70               run_callbacks :teardown, :enumerator => :reverse_each
71             rescue Test::Unit::AssertionFailedError => e
72               add_failure(e.message, e.backtrace)
73             rescue StandardError, ScriptError
74               add_error($!)
75             end
76           end
77         ensure
78           mocha_teardown
79         end
80         result.add_run
81         yield(Test::Unit::TestCase::FINISHED, name)
82       end
83     end
84   end
85 end
Note: See TracBrowser for help on using the browser.