Blob of rspec/lib/spec/example/configuration.rb (raw blob data)

1 module Spec
2 module Example
3 class Configuration
4 # Chooses what mock framework to use. Example:
5 #
6 # Spec::Runner.configure do |config|
7 # config.mock_with :rspec, :mocha, :flexmock, or :rr
8 # end
9 #
10 # To use any other mock framework, you'll have to provide your own
11 # adapter. This is simply a module that responds to the following
12 # methods:
13 #
14 # setup_mocks_for_rspec
15 # verify_mocks_for_rspec
16 # teardown_mocks_for_rspec.
17 #
18 # These are your hooks into the lifecycle of a given example. RSpec will
19 # call setup_mocks_for_rspec before running anything else in each
20 # Example. After executing the #after methods, RSpec will then call
21 # verify_mocks_for_rspec and teardown_mocks_for_rspec (this is
22 # guaranteed to run even if there are failures in
23 # verify_mocks_for_rspec).
24 #
25 # Once you've defined this module, you can pass that to mock_with:
26 #
27 # Spec::Runner.configure do |config|
28 # config.mock_with MyMockFrameworkAdapter
29 # end
30 #
31 def mock_with(mock_framework)
32 @mock_framework = case mock_framework
33 when Symbol
34 mock_framework_path(mock_framework.to_s)
35 else
36 mock_framework
37 end
38 end
39
40 def mock_framework # :nodoc:
41 @mock_framework ||= mock_framework_path("rspec")
42 end
43
44 # :call-seq:
45 # include(Some::Helpers)
46 # include(Some::Helpers, More::Helpers)
47 # include(My::Helpers, :type => :key)
48 #
49 # Declares modules to be included in multiple example groups
50 # (<tt>describe</tt> blocks). With no :type, the modules listed will be
51 # included in all example groups. Use :type to restrict the inclusion to
52 # a subset of example groups. The value assigned to :type should be a
53 # key that maps to a class that is either a subclass of
54 # Spec::Example::ExampleGroup or extends Spec::Example::ExampleGroupMethods
55 # and includes Spec::Example::ExampleMethods
56 #
57 # config.include(My::Pony, My::Horse, :type => :farm)
58 #
59 # Only example groups that have that type will get the modules included:
60 #
61 # describe "Downtown", :type => :city do
62 # # Will *not* get My::Pony and My::Horse included
63 # end
64 #
65 # describe "Old Mac Donald", :type => :farm do
66 # # *Will* get My::Pony and My::Horse included
67 # end
68 #
69 def include(*args)
70 args << {} unless Hash === args.last
71 modules, options = args_and_options(*args)
72 required_example_group = get_type_from_options(options)
73 required_example_group = required_example_group.to_sym if required_example_group
74 modules.each do |mod|
75 ExampleGroupFactory.get(required_example_group).send(:include, mod)
76 end
77 end
78
79 # Defines global predicate matchers. Example:
80 #
81 # config.predicate_matchers[:swim] = :can_swim?
82 #
83 # This makes it possible to say:
84 #
85 # person.should swim # passes if person.can_swim? returns true
86 #
87 def predicate_matchers
88 @predicate_matchers ||= {}
89 end
90
91 # Prepends a global <tt>before</tt> block to all example groups.
92 # See #append_before for filtering semantics.
93 def prepend_before(*args, &proc)
94 scope, options = scope_and_options(*args)
95 example_group = ExampleGroupFactory.get(
96 get_type_from_options(options)
97 )
98 example_group.prepend_before(scope, &proc)
99 end
100
101 # Appends a global <tt>before</tt> block to all example groups.
102 #
103 # If you want to restrict the block to a subset of all the example
104 # groups then specify this in a Hash as the last argument:
105 #
106 # config.prepend_before(:all, :type => :farm)
107 #
108 # or
109 #
110 # config.prepend_before(:type => :farm)
111 #
112 def append_before(*args, &proc)
113 scope, options = scope_and_options(*args)
114 example_group = ExampleGroupFactory.get(
115 get_type_from_options(options)
116 )
117 example_group.append_before(scope, &proc)
118 end
119 alias_method :before, :append_before
120
121 # Prepends a global <tt>after</tt> block to all example groups.
122 # See #append_before for filtering semantics.
123 def prepend_after(*args, &proc)
124 scope, options = scope_and_options(*args)
125 example_group = ExampleGroupFactory.get(
126 get_type_from_options(options)
127 )
128 example_group.prepend_after(scope, &proc)
129 end
130 alias_method :after, :prepend_after
131
132 # Appends a global <tt>after</tt> block to all example groups.
133 # See #append_before for filtering semantics.
134 def append_after(*args, &proc)
135 scope, options = scope_and_options(*args)
136 example_group = ExampleGroupFactory.get(
137 get_type_from_options(options)
138 )
139 example_group.append_after(scope, &proc)
140 end
141
142 private
143
144 def scope_and_options(*args)
145 args, options = args_and_options(*args)
146 scope = (args[0] || :each), options
147 end
148
149 def get_type_from_options(options)
150 options[:type] || options[:behaviour_type]
151 end
152
153 def mock_framework_path(framework_name)
154 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "plugins", "mock_frameworks", framework_name))
155 end
156 end
157 end
158 end