Module: Spec::Example::ExampleGroupMethods
- Includes:
- BeforeAndAfterHooks
- Included in:
- ExampleGroup, SharedExampleGroup, Test::Unit::TestCase
- Defined in:
- lib/gems/rspec-1.1.12/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_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
-
#reset ⇒ Object
Only used from RSpec’s own examples.
- #run(run_options) ⇒ 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
. - #subject_block ⇒ 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/gems/rspec-1.1.12/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.
22 23 24 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 22 def @description_options end |
#spec_path ⇒ Object (readonly)
Returns the value of attribute spec_path.
22 23 24 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 22 def spec_path @spec_path end |
Class Method Details
.description_text(*args) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 8 def description_text(*args) args.inject("") do |description, arg| description << " " unless (description == "" || arg.to_s =~ /^(\s|\.|#)/) description << arg.to_s end end |
.example_group_creation_listeners ⇒ Object
15 16 17 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 15 def example_group_creation_listeners @example_group_creation_listeners ||= [] end |
Instance Method Details
#all_before_each_parts ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 242 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.
26 27 28 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 26 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 93 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:
83 84 85 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 83 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
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 68 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_type ⇒ Object
180 181 182 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 180 def described_type description_parts.reverse.find {|part| part.is_a?(Module)} end |
#description ⇒ Object
175 176 177 178 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 175 def description result = ExampleGroupMethods.description_text(*description_parts) (result.nil? || result == "") ? to_s : result end |
#description_args ⇒ Object
39 40 41 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 39 def description_args @description_args ||= [] end |
#description_parts ⇒ Object
:nodoc:
204 205 206 207 208 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 204 def description_parts #:nodoc: 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.
144 145 146 147 148 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 144 def example(description=nil, ={}, &implementation) e = new(description, , &implementation) example_objects << e e end |
#example_group_backtrace ⇒ Object
Deprecated - use backtrace()
31 32 33 34 35 36 37 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 31 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:
220 221 222 223 224 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 220 def examples(=nil) #:nodoc: examples = example_objects.dup add_method_examples(examples) ( && .reverse) ? examples.reverse : examples end |
#inherited(klass) ⇒ Object
43 44 45 46 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 43 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.
104 105 106 107 108 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 104 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:
226 227 228 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 226 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 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
138 139 140 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 138 def predicate_matchers @predicate_matchers ||= {} end |
#register_example_group(klass) ⇒ Object
48 49 50 51 52 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 48 def register_example_group(klass) ExampleGroupMethods.example_group_creation_listeners.each do |l| l.register_example_group(klass) end end |
#reset ⇒ Object
Only used from RSpec’s own examples
231 232 233 234 235 236 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 231 def reset # :nodoc: @before_all_parts = nil @after_all_parts = nil @before_each_parts = nil @after_each_parts = nil end |
#run(run_options) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 161 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
252 253 254 255 256 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 252 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
238 239 240 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 238 def run_before_each(example) example.eval_each_fail_fast(all_before_each_parts) end |
#set_description(*args) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 210 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 |
#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.
196 197 198 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 196 def subject(&block) @_subject_block = block end |
#subject_block ⇒ Object
200 201 202 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 200 def subject_block @_subject_block || lambda {nil} end |
#xexample(description = nil, opts = {}, &block) ⇒ Object Also known as: xit, xspecify
Use this to temporarily disable an example.
154 155 156 |
# File 'lib/gems/rspec-1.1.12/lib/spec/example/example_group_methods.rb', line 154 def xexample(description=nil, opts={}, &block) Kernel.warn("Example disabled: #{description}") end |