Module: Spec::Example::ExampleGroupMethods
- Includes:
- BeforeAndAfterHooks
- Included in:
- ExampleGroup, SharedExampleGroup, Test::Unit::TestCase
- Defined in:
- lib/spec/example/example_group_methods.rb
Class Attribute Summary collapse
-
.matcher_class ⇒ Object
Returns the value of attribute matcher_class.
Instance Attribute Summary collapse
-
#description_options ⇒ Object
(also: #options)
readonly
Returns the value of attribute description_options.
-
#description_text ⇒ Object
readonly
Returns the value of attribute description_text.
-
#spec_path ⇒ Object
readonly
Returns the value of attribute spec_path.
Class Method Summary collapse
Instance Method Summary collapse
- #create_nested_example_group(*args, &example_group_block) ⇒ Object
- #create_shared_example_group(*args, &example_group_block) ⇒ Object
-
#describe(*args, &example_group_block) ⇒ Object
(also: #context)
Makes the describe/it syntax available from a class.
- #described_type ⇒ Object
- #description ⇒ Object
- #description_args ⇒ Object
-
#description_parts ⇒ Object
:nodoc:.
-
#example(description = nil, options = {}, &implementation) ⇒ Object
(also: #it, #specify)
Creates an instance of the current example group class and adds it to a collection of examples of the current example group.
-
#examples ⇒ Object
:nodoc:.
- #inherited(klass) ⇒ Object
-
#it_should_behave_like(*shared_example_groups) ⇒ Object
Use this to pull in examples from shared example groups.
-
#number_of_examples ⇒ Object
:nodoc:.
- #predicate_matchers ⇒ Object
- #register(®istration_binding_block) ⇒ Object
- #registration_backtrace ⇒ Object
-
#reset ⇒ Object
Only used from RSpec’s own examples.
- #run ⇒ Object
- #run_after_each(example) ⇒ Object
- #run_before_each(example) ⇒ Object
- #set_description(*args) ⇒ Object
-
#subject(&block) ⇒ Object
Defines an explicit subject for an example group which can then be the implicit receiver (through delegation) of calls to
should. -
#unregister ⇒ Object
:nodoc:.
-
#xexample(description = nil, opts = {}, &block) ⇒ Object
(also: #xit, #xspecify)
Use this to temporarily disable an example.
Methods included from BeforeAndAfterHooks
#after_all_parts, #after_each_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, #prepend_after, #prepend_before, #remove_after, #setup, #teardown
Class Attribute Details
.matcher_class ⇒ Object
Returns the value of attribute matcher_class.
8 9 10 |
# File 'lib/spec/example/example_group_methods.rb', line 8 def matcher_class @matcher_class end |
Instance Attribute Details
#description_options ⇒ Object (readonly) Also known as: options
Returns the value of attribute description_options.
18 19 20 |
# File 'lib/spec/example/example_group_methods.rb', line 18 def @description_options end |
#description_text ⇒ Object (readonly)
Returns the value of attribute description_text.
18 19 20 |
# File 'lib/spec/example/example_group_methods.rb', line 18 def description_text @description_text end |
#spec_path ⇒ Object (readonly)
Returns the value of attribute spec_path.
18 19 20 |
# File 'lib/spec/example/example_group_methods.rb', line 18 def spec_path @spec_path end |
Class Method Details
.description_text(*args) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/spec/example/example_group_methods.rb', line 10 def description_text(*args) args.inject("") do |result, arg| result << " " unless (result == "" || arg.to_s =~ /^(\s|\.|#)/) result << arg.to_s end end |
Instance Method Details
#create_nested_example_group(*args, &example_group_block) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/spec/example/example_group_methods.rb', line 65 def create_nested_example_group(*args, &example_group_block) self.subclass("Subclass") do set_description(*args) module_eval(&example_group_block) end end |
#create_shared_example_group(*args, &example_group_block) ⇒ Object
61 62 63 |
# File 'lib/spec/example/example_group_methods.rb', line 61 def create_shared_example_group(*args, &example_group_block) SharedExampleGroup.register(*args, &example_group_block) end |
#describe(*args, &example_group_block) ⇒ Object Also known as: context
Makes the describe/it syntax available from a class. For example:
class StackSpec < Spec::ExampleGroup
describe Stack, "with no elements"
before
@stack = Stack.new
end
it "should raise on pop" do
lambda{ @stack.pop }.should raise_error
end
end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/spec/example/example_group_methods.rb', line 45 def describe(*args, &example_group_block) args << {} unless Hash === args.last if example_group_block = args.last [:spec_path] = eval("caller(0)[1]", example_group_block) unless [:spec_path] if [:shared] create_shared_example_group(*args, &example_group_block) else create_nested_example_group(*args, &example_group_block) end else set_description(*args) end end |
#described_type ⇒ Object
150 151 152 |
# File 'lib/spec/example/example_group_methods.rb', line 150 def described_type description_parts.reverse.find {|part| part.is_a?(Module)} end |
#description ⇒ Object
145 146 147 148 |
# File 'lib/spec/example/example_group_methods.rb', line 145 def description result = ExampleGroupMethods.description_text(*description_parts) (result.nil? || result == "") ? to_s : result end |
#description_args ⇒ Object
21 22 23 |
# File 'lib/spec/example/example_group_methods.rb', line 21 def description_args @description_args ||= [] end |
#description_parts ⇒ Object
:nodoc:
174 175 176 177 178 179 180 |
# File 'lib/spec/example/example_group_methods.rb', line 174 def description_parts #:nodoc: parts = [] each_ancestor_example_group_class do |example_group_class| parts << example_group_class.description_args end parts.flatten.compact end |
#example(description = nil, options = {}, &implementation) ⇒ Object Also known as: it, specify
Creates an instance of the current example group class and adds it to a collection of examples of the current example group.
114 115 116 117 118 |
# File 'lib/spec/example/example_group_methods.rb', line 114 def example(description=nil, ={}, &implementation) e = new(description, , &implementation) example_objects << e e end |
#examples ⇒ Object
:nodoc:
191 192 193 194 195 |
# File 'lib/spec/example/example_group_methods.rb', line 191 def examples #:nodoc: examples = example_objects.dup add_method_examples(examples) Spec::Runner..reverse ? examples.reverse : examples end |
#inherited(klass) ⇒ Object
25 26 27 28 29 |
# File 'lib/spec/example/example_group_methods.rb', line 25 def inherited(klass) super klass.register {} Spec::Runner.register_at_exit_hook end |
#it_should_behave_like(*shared_example_groups) ⇒ Object
Use this to pull in examples from shared example groups. See Spec::Runner for information about shared example groups.
74 75 76 77 78 |
# File 'lib/spec/example/example_group_methods.rb', line 74 def it_should_behave_like(*shared_example_groups) shared_example_groups.each do |group| include_shared_example_group(group) end end |
#number_of_examples ⇒ Object
:nodoc:
197 198 199 |
# File 'lib/spec/example/example_group_methods.rb', line 197 def number_of_examples #:nodoc: examples.length end |
#predicate_matchers ⇒ Object
:call-seq:
predicate_matchers[matcher_name] = method_on_object
predicate_matchers[matcher_name] = [method1_on_object, method2_on_object]
Dynamically generates a custom matcher that will match a predicate on your class. RSpec provides a couple of these out of the box:
exist (or state expectations)
File.should exist("path/to/file")
an_instance_of (for mock argument constraints)
mock.should_receive(:message).with(an_instance_of(String))
Examples
class Fish
def can_swim?
true
end
end
describe Fish do
predicate_matchers[:swim] = :can_swim?
it "should swim" do
Fish.new.should swim
end
end
108 109 110 |
# File 'lib/spec/example/example_group_methods.rb', line 108 def predicate_matchers @predicate_matchers ||= {:an_instance_of => :is_a?} end |
#register(®istration_binding_block) ⇒ Object
209 210 211 212 |
# File 'lib/spec/example/example_group_methods.rb', line 209 def register(®istration_binding_block) @registration_binding_block = registration_binding_block Spec::Runner..add_example_group self end |
#registration_backtrace ⇒ Object
218 219 220 |
# File 'lib/spec/example/example_group_methods.rb', line 218 def registration_backtrace eval("caller", @registration_binding_block) end |
#reset ⇒ Object
Only used from RSpec’s own examples
202 203 204 205 206 207 |
# File 'lib/spec/example/example_group_methods.rb', line 202 def reset # :nodoc: @before_all_parts = nil @after_all_parts = nil @before_each_parts = nil @after_each_parts = nil end |
#run ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/spec/example/example_group_methods.rb', line 131 def run examples = examples_to_run reporter.add_example_group(self) unless examples_to_run.empty? return true if examples.empty? return dry_run(examples) if dry_run? plugin_mock_framework define_methods_from_predicate_matchers success, before_all_instance_variables = run_before_all success, after_all_instance_variables = execute_examples(success, before_all_instance_variables, examples) success = run_after_all(success, after_all_instance_variables) end |
#run_after_each(example) ⇒ Object
228 229 230 231 232 |
# File 'lib/spec/example/example_group_methods.rb', line 228 def run_after_each(example) each_ancestor_example_group_class(:superclass_first) do |example_group_class| example.eval_each_fail_slow(example_group_class.after_each_parts) end end |
#run_before_each(example) ⇒ Object
222 223 224 225 226 |
# File 'lib/spec/example/example_group_methods.rb', line 222 def run_before_each(example) each_ancestor_example_group_class do |example_group_class| example.eval_each_fail_fast(example_group_class.before_each_parts) end end |
#set_description(*args) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/spec/example/example_group_methods.rb', line 182 def set_description(*args) args, = Spec::Example.(*args) @description_args = args @description_options = @description_text = ExampleGroupMethods.description_text(*args) @spec_path = File.([:spec_path]) if [:spec_path] self end |
#subject(&block) ⇒ Object
Defines an explicit subject for an example group which can then be the implicit receiver (through delegation) of calls to should.
Examples
describe CheckingAccount, "with $50" do
subject { CheckingAccount.new(:amount => 50, :currency => :USD) }
it { should have_a_balance_of(50, :USD)}
it { should_not be_overdrawn}
end
See ExampleMethods#should for more information about this approach.
166 167 168 169 170 171 172 |
# File 'lib/spec/example/example_group_methods.rb', line 166 def subject(&block) if block @_subject_block = block else @_subject_block.call if @_subject_block end end |
#unregister ⇒ Object
:nodoc:
214 215 216 |
# File 'lib/spec/example/example_group_methods.rb', line 214 def unregister #:nodoc: Spec::Runner..remove_example_group self end |
#xexample(description = nil, opts = {}, &block) ⇒ Object Also known as: xit, xspecify
Use this to temporarily disable an example.
124 125 126 |
# File 'lib/spec/example/example_group_methods.rb', line 124 def xexample(description=nil, opts={}, &block) Kernel.warn("Example disabled: #{description}") end |