public
Description: Tracks is a GTD(TM) web application, built with Ruby on Rails
Homepage: http://www.rousette.org.uk/projects/
Clone URL: git://github.com/bsag/tracks.git
Click here to lend your support to: tracks and make a donation at www.pledgie.com !
lrbalt (author)
Tue Jun 24 11:35:30 -0700 2008
commit  ce1c09217327e82c06ef5dc456c2a700258f18e6
tree    59f817d19cd3c143398471b69b515e05a09bfa8f
parent  c58186451f6f6ec1116cfac3c131ba3ae5dc88ea
tracks / vendor / plugins / scenarios / lib / scenarios / extensions / test_case.rb
100644 77 lines (70 sloc) 3.1 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Test #:nodoc:
  module Unit #:nodoc:
    class TestCase #:nodoc:
      superclass_delegating_accessor :scenario_classes
      superclass_delegating_accessor :table_config
      
      # Changing either of these is not supported at this time.
      self.use_transactional_fixtures = true
      self.use_instantiated_fixtures = false
      
      include Scenarios::TableMethods
      include Scenarios::Loading
      
      class << self
        # This class method is mixed into RSpec and allows you to declare that
        # you are using a given scenario or set of scenarios within a spec:
        #
        # scenario :basic # loads BasicScenario and any dependencies
        # scenario :posts, :comments # loads PostsScenario and CommentsScenario
        #
        # It accepts an array of scenarios (strings, symbols, or classes) and
        # will load them roughly in the order that they are specified.
        def scenario(*names)
          self.scenario_classes = []
          names.each do |name|
            scenario_class = name.to_scenario
            scenario_classes.concat(scenario_class.used_scenarios + [scenario_class])
          end
          scenario_classes.uniq!
        end
      
        # Overridden to provide before all and after all code which sets up and
        # tears down scenarios
        def suite_with_scenarios
          suite = suite_without_scenarios
          class << suite
            attr_accessor :test_class
            def run_with_scenarios(*args, &block)
              debugger
              run_without_scenarios(*args, &block)
              test_class.table_config.loaded_scenarios.each { |s| s.unload } if test_class.table_config
            end
            alias_method_chain :run, :scenarios
          end
          suite.test_class = self
          suite
        end
        alias_method_chain :suite, :scenarios
      end
      
      # Hook into fixtures loading lifecycle to instead load scenarios. This
      # is expected to be called in a fashion respective of
      # use_transactional_fixtures. I feel like a leech.
      def load_fixtures
        if !scenarios_loaded? || !use_transactional_fixtures?
          self.class.table_config = Scenarios::Configuration.new if !use_transactional_fixtures? || table_config.nil?
          load_scenarios(scenario_classes)
        end
        self.extend scenario_helpers
        self.extend table_readers
      end
      
      # Here we are counting on existing logic to allow teardown method
      # overriding as done in fixtures.rb. Only if transaction fixtures are
      # not being used do we unload scenarios after a test. Otherwise, we wait
      # until the end of the run of all tests on this test_case (collection of
      # tests, right!). See the TestSuite extension done in _suite_ for
      # behaviour when using transaction fixtures.
      def teardown_with_scenarios
        teardown_without_scenarios
        loaded_scenarios.each { |s| s.unload } unless use_transactional_fixtures?
      end
      alias_method_chain :teardown, :scenarios
      
    end
  end
end