Module: Spec::Example::ExampleGroupMethods
- Includes:
- BeforeAndAfterHooks, Subject::ExampleGroupMethods
- Included in:
- ExampleGroup, SharedExampleGroup, Test::Unit::TestCase
- Defined in:
- lib/spec/example/example_group_methods.rb
Defined Under Namespace
Classes: ExampleGroupHierarchy
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.
-
#spec_path ⇒ Object
readonly
Returns the value of attribute spec_path.
Class Method Summary collapse
Instance Method Summary collapse
- #all_before_each_parts ⇒ Object
-
#backtrace ⇒ Object
Provides the backtrace up to where this example_group was declared.
-
#create_example_group_subclass(*args, &example_group_block) ⇒ Object
Creates a new subclass of self, with a name “under” our own name.
-
#create_shared_example_group(*args, &example_group_block) ⇒ Object
:nodoc:.
-
#describe(*args, &example_group_block) ⇒ Object
(also: #context)
Makes the describe/it syntax available from a class.
- #described_class ⇒ Object
- #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.
-
#example_group_backtrace ⇒ Object
Deprecated - use backtrace().
-
#examples(run_options = nil) ⇒ 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
:call-seq: predicate_matchers = method_on_object predicate_matchers = [method1_on_object, method2_on_object].
- #register_example_group(klass) ⇒ Object
- #run(run_options) ⇒ Object
- #run_after_each(example) ⇒ Object
- #run_before_each(example) ⇒ Object
- #set_description(*args) ⇒ Object
-
#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, after_suite_parts, #after_suite_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, #before_suite_parts, before_suite_parts, #prepend_after, #prepend_before, #remove_after, #setup, #teardown
Class Attribute Details
.matcher_class ⇒ Object
Returns the value of attribute matcher_class.
6 7 8 |
# File 'lib/spec/example/example_group_methods.rb', line 6 def matcher_class @matcher_class end |
Instance Attribute Details
#description_options ⇒ Object (readonly) Also known as: options
Returns the value of attribute description_options.
24 25 26 |
# File 'lib/spec/example/example_group_methods.rb', line 24 def @description_options end |
#spec_path ⇒ Object (readonly)
Returns the value of attribute spec_path.
24 25 26 |
# File 'lib/spec/example/example_group_methods.rb', line 24 def spec_path @spec_path end |
Class Method Details
.description_text(*args) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/spec/example/example_group_methods.rb', line 8 def description_text(*args) text = args.inject("") do |description, arg| description << " " unless (description == "" || arg.to_s =~ /^(\s|\.|#)/) description << arg.to_s end text == "" ? nil : text end |
.example_group_creation_listeners ⇒ Object
16 17 18 |
# File 'lib/spec/example/example_group_methods.rb', line 16 def example_group_creation_listeners @example_group_creation_listeners ||= [] end |
Instance Method Details
#all_before_each_parts ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/spec/example/example_group_methods.rb', line 219 def all_before_each_parts unless @all_before_each_parts @all_before_each_parts = [] example_group_hierarchy.each do |example_group_class| @all_before_each_parts += example_group_class.before_each_parts end end @all_before_each_parts end |
#backtrace ⇒ Object
Provides the backtrace up to where this example_group was declared.
39 40 41 |
# File 'lib/spec/example/example_group_methods.rb', line 39 def backtrace @backtrace end |
#create_example_group_subclass(*args, &example_group_block) ⇒ Object
Creates a new subclass of self, with a name “under” our own name. Example:
x = Foo::Bar.subclass('Zap'){}
x.name # => Foo::Bar::Zap_1
x.superclass.name # => Foo::Bar
95 96 97 98 99 100 101 102 103 |
# File 'lib/spec/example/example_group_methods.rb', line 95 def create_example_group_subclass(*args, &example_group_block) # :nodoc: @class_count ||= 0 @class_count += 1 klass = const_set("Subclass_#{@class_count}", Class.new(self)) klass.set_description(*args) example_group_block = ExampleGroupFactory.include_constants_in(args.last[:scope], &example_group_block) klass.module_eval(&example_group_block) klass end |
#create_shared_example_group(*args, &example_group_block) ⇒ Object
:nodoc:
85 86 87 |
# File 'lib/spec/example/example_group_methods.rb', line 85 def create_shared_example_group(*args, &example_group_block) # :nodoc: 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
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/spec/example/example_group_methods.rb', line 70 def describe(*args, &example_group_block) if example_group_block Spec::Example::add_spec_path_to(args) = args.last if [:shared] create_shared_example_group(*args, &example_group_block) else create_example_group_subclass(*args, &example_group_block) end else set_description(*args) end end |
#described_class ⇒ Object
185 186 187 |
# File 'lib/spec/example/example_group_methods.rb', line 185 def described_class Class === described_type ? described_type : nil end |
#described_type ⇒ Object
181 182 183 |
# File 'lib/spec/example/example_group_methods.rb', line 181 def described_type @described_type ||= description_parts.reverse.find {|part| part.is_a?(Module)} end |
#description ⇒ Object
177 178 179 |
# File 'lib/spec/example/example_group_methods.rb', line 177 def description @description ||= ExampleGroupMethods.description_text(*description_parts) || to_s end |
#description_args ⇒ Object
52 53 54 |
# File 'lib/spec/example/example_group_methods.rb', line 52 def description_args @description_args ||= [] end |
#description_parts ⇒ Object
:nodoc:
189 190 191 192 193 |
# File 'lib/spec/example/example_group_methods.rb', line 189 def description_parts #:nodoc: @description_parts ||= example_group_hierarchy.inject([]) do |parts, example_group_class| [parts << example_group_class.description_args].flatten end 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.
146 147 148 149 150 |
# File 'lib/spec/example/example_group_methods.rb', line 146 def example(description=nil, ={}, &implementation) e = new(description, , &implementation) example_objects << e e end |
#example_group_backtrace ⇒ Object
Deprecated - use backtrace()
44 45 46 47 48 49 50 |
# File 'lib/spec/example/example_group_methods.rb', line 44 def example_group_backtrace Kernel.warn <<-WARNING ExampleGroupMethods#example_group_backtrace is deprecated and will be removed from a future version. Please use ExampleGroupMethods#backtrace instead. WARNING backtrace end |
#examples(run_options = nil) ⇒ Object
:nodoc:
205 206 207 208 209 |
# File 'lib/spec/example/example_group_methods.rb', line 205 def examples(=nil) #:nodoc: examples = example_objects.dup add_method_examples(examples) ( && .reverse) ? examples.reverse : examples end |
#inherited(klass) ⇒ Object
27 28 29 30 |
# File 'lib/spec/example/example_group_methods.rb', line 27 def inherited(klass) super register_example_group(klass) end |
#it_should_behave_like(*shared_example_groups) ⇒ Object
Use this to pull in examples from shared example groups.
106 107 108 109 110 |
# File 'lib/spec/example/example_group_methods.rb', line 106 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:
211 212 213 |
# File 'lib/spec/example/example_group_methods.rb', line 211 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 (for state expectations)
File.should exist("path/to/file")
an_instance_of (for mock argument matchers)
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
140 141 142 |
# File 'lib/spec/example/example_group_methods.rb', line 140 def predicate_matchers @predicate_matchers ||= {} end |
#register_example_group(klass) ⇒ Object
32 33 34 35 36 |
# File 'lib/spec/example/example_group_methods.rb', line 32 def register_example_group(klass) ExampleGroupMethods.example_group_creation_listeners.each do |listener| listener.register_example_group(klass) end end |
#run(run_options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/spec/example/example_group_methods.rb', line 163 def run() examples = examples_to_run() .reporter.add_example_group(self) unless examples.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
229 230 231 232 233 |
# File 'lib/spec/example/example_group_methods.rb', line 229 def run_after_each(example) example_group_hierarchy.reverse.each do |example_group_class| example.eval_each_fail_slow(example_group_class.after_each_parts) end end |
#run_before_each(example) ⇒ Object
215 216 217 |
# File 'lib/spec/example/example_group_methods.rb', line 215 def run_before_each(example) example.eval_each_fail_fast(all_before_each_parts) end |
#set_description(*args) ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/spec/example/example_group_methods.rb', line 195 def set_description(*args) args, = Spec::Example.(*args) @description_args = args @description_options = @description_text = ExampleGroupMethods.description_text(*args) @backtrace = caller(1) @spec_path = File.([:spec_path]) if [:spec_path] self end |
#xexample(description = nil, opts = {}, &block) ⇒ Object Also known as: xit, xspecify
Use this to temporarily disable an example.
156 157 158 |
# File 'lib/spec/example/example_group_methods.rb', line 156 def xexample(description=nil, opts={}, &block) Kernel.warn("Example disabled: #{description}") end |