Class: RSpec::Core::ExampleGroup

Overview

ExampleGroup and Example are the main structural elements of rspec-core. Consider this example:

describe Thing do
  it "does something" do
  end
end

The object returned by ‘describe Thing` is a subclass of ExampleGroup. The object returned by `it “does something”` is an instance of Example, which serves as a wrapper for an instance of the ExampleGroup in which it is declared.

Constant Summary

Constants included from Pending

Pending::NOT_YET_IMPLEMENTED, Pending::NO_REASON_GIVEN

Instance Attribute Summary collapse

Attributes included from Subject::ExampleGroupMethods

#explicit_subject_block

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetadataHashBuilder::Common

#build_metadata_hash_from

Methods included from RSpec::Core::Extensions::ModuleEvalWithArgs

module_eval_with_args

Methods included from RSpec::Core::Extensions::InstanceEvalWithArgs

#instance_eval_with_args

Methods included from Subject::ExampleGroupMethods

its, subject

Methods included from Hooks

after, around, before, find_hook, hooks, run_hook, run_hook!, run_hook_filtered

Methods included from Let

included

Methods included from Pending

#pending

Methods included from Subject::ExampleMethods

#subject

Instance Attribute Details

#exampleObject

Returns the [Example](Example) object that wraps this instance of ‘ExampleGroup`



408
409
410
# File 'lib/rspec/core/example_group.rb', line 408

def example
  @example
end

Class Method Details

.all_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
# File 'lib/rspec/core/example_group.rb', line 384

def self.all_apply?(filters)
  .all_apply?(filters)
end

.ancestorsObject



216
217
218
# File 'lib/rspec/core/example_group.rb', line 216

def self.ancestors
  @_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup}
end

.any_apply?(filters) ⇒ Boolean

Returns:

  • (Boolean)


379
380
381
# File 'lib/rspec/core/example_group.rb', line 379

def self.any_apply?(filters)
  .any_apply?(filters)
end

.around_hooks_for(example) ⇒ Object



323
324
325
# File 'lib/rspec/core/example_group.rb', line 323

def self.around_hooks_for(example)
  world.find_hook(:around, :each, self, example) + ancestors.reverse.inject([]){|l,a| l + a.find_hook(:around, :each, self, example)}
end

.assign_before_all_ivars(ivars, example_group_instance) ⇒ Object



266
267
268
269
# File 'lib/rspec/core/example_group.rb', line 266

def self.assign_before_all_ivars(ivars, example_group_instance)
  return if ivars.empty?
  ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) }
end

.before_all_ivarsObject



253
254
255
# File 'lib/rspec/core/example_group.rb', line 253

def self.before_all_ivars
  @before_all_ivars ||= {}
end

.block_not_supported(label) ⇒ Object



117
118
119
# File 'lib/rspec/core/example_group.rb', line 117

def self.block_not_supported(label)
  warn("Customization blocks not supported for include_#{label}.  Use it_behaves_like instead.")
end

.childrenObject



206
207
208
# File 'lib/rspec/core/example_group.rb', line 206

def self.children
  @children ||= [].extend(Extensions::Ordered)
end

.declaration_line_numbersObject



389
390
391
392
393
# File 'lib/rspec/core/example_group.rb', line 389

def self.declaration_line_numbers
  @declaration_line_numbers ||= [[:example_group][:line_number]] +
    examples.collect {|e| e.[:line_number]} +
    children.inject([]) {|l,c| l + c.declaration_line_numbers}
end

.define_example_method(name, extra_options = {}) ⇒ Object Also known as: alias_example_to



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/core/example_group.rb', line 53

def self.define_example_method(name, extra_options={})
  module_eval(<<-END_RUBY, __FILE__, __LINE__)
    def self.#{name}(desc=nil, *args, &block)
      options = build_metadata_hash_from(args)
      options.update(:pending => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
      options.update(#{extra_options.inspect})
      examples << RSpec::Core::Example.new(self, desc, options, block)
      examples.last
    end
  END_RUBY
end

.define_nested_shared_group_method(new_name, report_label = nil) ⇒ Object Also known as: alias_it_should_behave_like_to



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rspec/core/example_group.rb', line 82

def self.define_nested_shared_group_method(new_name, report_label=nil)
  module_eval(<<-END_RUBY, __FILE__, __LINE__)
    def self.#{new_name}(name, *args, &customization_block)
      group = describe("#{report_label || "it should behave like"} \#{name}") do
        find_and_eval_shared("examples", name, *args, &customization_block)
      end
      group.metadata[:shared_group_name] = name
      group
    end
  END_RUBY
end

.delegate_to_metadata(*names) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rspec/core/example_group.rb', line 38

def self.(*names)
  names.each do |name|
    define_method name do
      [:example_group][name]
    end
  end
end

.descendant_filtered_examplesObject



141
142
143
# File 'lib/rspec/core/example_group.rb', line 141

def self.descendant_filtered_examples
  @descendant_filtered_examples ||= filtered_examples + children.inject([]){|l,c| l + c.descendant_filtered_examples}
end

.descendantsObject



211
212
213
# File 'lib/rspec/core/example_group.rb', line 211

def self.descendants
  @_descendants ||= [self] + children.inject([]) {|list, c| list + c.descendants}
end

.describe(*args, &example_group_block) ⇒ Object Also known as: context

Generates a subclass of this example group which inherits everything except the examples themselves.

## Examples

describe "something" do # << This describe method is defined in
                        # << RSpec::Core::DSL, included in the
                        # << global namespace
  before do
    do_something_before
  end

  let(:thing) { Thing.new }

  describe "attribute (of something)" do
    # examples in the group get the before hook
    # declared above, and can access `thing`
  end
end

See Also:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rspec/core/example_group.rb', line 178

def self.describe(*args, &example_group_block)
  @_subclass_count ||= 0
  @_subclass_count += 1
  args << {} unless args.last.is_a?(Hash)
  args.last.update(:example_group_block => example_group_block)

  # TODO 2010-05-05: Because we don't know if const_set is thread-safe
  child = const_set(
    "Nested_#{@_subclass_count}",
    subclass(self, args, &example_group_block)
  )
  children << child
  child
end

.describesObject



49
# File 'lib/rspec/core/example_group.rb', line 49

alias_method :describes, :described_class

.ensure_example_groups_are_configuredObject



226
227
228
229
230
231
232
# File 'lib/rspec/core/example_group.rb', line 226

def self.ensure_example_groups_are_configured
  unless defined?(@@example_groups_configured)
    RSpec.configuration.configure_mock_framework
    RSpec.configuration.configure_expectation_framework
    @@example_groups_configured = true
  end
end

.examplesObject



131
132
133
# File 'lib/rspec/core/example_group.rb', line 131

def self.examples
  @examples ||= []
end

.fail_fast?Boolean

Returns:

  • (Boolean)


374
375
376
# File 'lib/rspec/core/example_group.rb', line 374

def self.fail_fast?
  RSpec.configuration.fail_fast?
end

.fail_filtered_examples(exception, reporter) ⇒ Object



362
363
364
365
366
367
368
369
370
371
# File 'lib/rspec/core/example_group.rb', line 362

def self.fail_filtered_examples(exception, reporter)
  filtered_examples.each { |example| example.fail_with_exception(reporter, exception) }

  children.each do |child|
    reporter.example_group_started(child)
    child.fail_filtered_examples(exception, reporter)
    reporter.example_group_finished(child)
  end
  false
end

.filtered_examplesObject



136
137
138
# File 'lib/rspec/core/example_group.rb', line 136

def self.filtered_examples
  world.filtered_examples[self]
end

.find_and_eval_shared(label, name, *args, &customization_block) ⇒ Object

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
# File 'lib/rspec/core/example_group.rb', line 122

def self.find_and_eval_shared(label, name, *args, &customization_block)
  raise ArgumentError, "Could not find shared #{label} #{name.inspect}" unless
    shared_block = world.shared_example_groups[name]

  module_eval_with_args(*args, &shared_block)
  module_eval(&customization_block) if customization_block
end

.include_context(name, *args) ⇒ Object

Includes shared content declared with ‘name`.

See Also:



105
106
107
# File 'lib/rspec/core/example_group.rb', line 105

def self.include_context(name, *args)
  block_given? ? block_not_supported("context") : find_and_eval_shared("context", name, *args)
end

.include_examples(name, *args) ⇒ Object

Includes shared content declared with ‘name`.

See Also:



112
113
114
# File 'lib/rspec/core/example_group.rb', line 112

def self.include_examples(name, *args)
  block_given? ? block_not_supported("examples") : find_and_eval_shared("examples", name, *args)
end

.metadataObject

The [Metadata](Metadata) object associated with this group.

See Also:



147
148
149
# File 'lib/rspec/core/example_group.rb', line 147

def self.
  @metadata if defined?(@metadata)
end

.registerObject



32
33
34
# File 'lib/rspec/core/example_group.rb', line 32

def self.register
  world.register(self)
end

.run(reporter) ⇒ Object

Runs all the examples in this group



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rspec/core/example_group.rb', line 328

def self.run(reporter)
  if RSpec.wants_to_quit
    RSpec.clear_remaining_example_groups if top_level?
    return
  end
  reporter.example_group_started(self)

  begin
    run_before_all_hooks(new)
    result_for_this_group = run_examples(reporter)
    results_for_descendants = children.ordered.map {|child| child.run(reporter)}.all?
    result_for_this_group && results_for_descendants
  rescue Exception => ex
    fail_filtered_examples(ex, reporter)
  ensure
    run_after_all_hooks(new)
    before_all_ivars.clear
    reporter.example_group_finished(self)
  end
end

.run_after_all_hooks(example_group_instance) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rspec/core/example_group.rb', line 302

def self.run_after_all_hooks(example_group_instance)
  return if descendant_filtered_examples.empty?
  assign_before_all_ivars(before_all_ivars, example_group_instance)

  begin
    run_hook!(:after, :all, example_group_instance)
  rescue => e
    # TODO: come up with a better solution for this.
    RSpec.configuration.reporter.message <<-EOS

An error occurred in an after(:all) hook.
  #{e.class}: #{e.message}
  occurred at #{e.backtrace.first}

  EOS
  end

  world.run_hook_filtered(:after, :all, self, example_group_instance)
end

.run_after_each_hooks(example) ⇒ Object



296
297
298
299
# File 'lib/rspec/core/example_group.rb', line 296

def self.run_after_each_hooks(example)
  ancestors.each { |ancestor| ancestor.run_hook(:after, :each, example.example_group_instance) }
  world.run_hook_filtered(:after, :each, self, example.example_group_instance, example)
end

.run_around_each_hooks(example, initial_procsy) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/rspec/core/example_group.rb', line 281

def self.run_around_each_hooks(example, initial_procsy)
  example.around_hooks.reverse.inject(initial_procsy) do |procsy, around_hook|
    Example.procsy(procsy.) do
      example.example_group_instance.instance_eval_with_args(procsy, &around_hook)
    end
  end
end

.run_before_all_hooks(example_group_instance) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/rspec/core/example_group.rb', line 272

def self.run_before_all_hooks(example_group_instance)
  return if descendant_filtered_examples.empty?
  assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
  world.run_hook_filtered(:before, :all, self, example_group_instance)
  run_hook!(:before, :all, example_group_instance)
  store_before_all_ivars(example_group_instance)
end

.run_before_each_hooks(example) ⇒ Object



290
291
292
293
# File 'lib/rspec/core/example_group.rb', line 290

def self.run_before_each_hooks(example)
  world.run_hook_filtered(:before, :each, self, example.example_group_instance, example)
  ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example.example_group_instance) }
end

.run_examples(reporter) ⇒ Object



350
351
352
353
354
355
356
357
358
359
# File 'lib/rspec/core/example_group.rb', line 350

def self.run_examples(reporter)
  filtered_examples.ordered.map do |example|
    next if RSpec.wants_to_quit
    instance = new
    set_ivars(instance, before_all_ivars)
    succeeded = example.run(instance, reporter)
    RSpec.wants_to_quit = true if fail_fast? && !succeeded
    succeeded
  end.all?
end

.set_it_up(*args) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rspec/core/example_group.rb', line 235

def self.set_it_up(*args)
  # Ruby 1.9 has a bug that can lead to infinite recursion and a
  # SystemStackError if you include a module in a superclass after
  # including it in a subclass: https://gist.github.com/845896
  # To prevent this, we must include any modules in RSpec::Core::ExampleGroup
  # before users create example groups and have a chance to include
  # the same module in a subclass of RSpec::Core::ExampleGroup.
  # So we need to configure example groups here.
  ensure_example_groups_are_configured

  symbol_description = args.shift if args.first.is_a?(Symbol)
  args << (args)
  args.unshift(symbol_description) if symbol_description
  @metadata = RSpec::Core::Metadata.new().process(*args)
  world.configure_group(self)
end

.set_ivars(instance, ivars) ⇒ Object



401
402
403
# File 'lib/rspec/core/example_group.rb', line 401

def self.set_ivars(instance, ivars)
  ivars.each {|name, value| instance.instance_variable_set(name, value)}
end

.store_before_all_ivars(example_group_instance) ⇒ Object



258
259
260
261
262
263
# File 'lib/rspec/core/example_group.rb', line 258

def self.store_before_all_ivars(example_group_instance)
  return if example_group_instance.instance_variables.empty?
  example_group_instance.instance_variables.each { |ivar|
    before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
  }
end

.subclass(parent, args, &example_group_block) ⇒ Object



198
199
200
201
202
203
# File 'lib/rspec/core/example_group.rb', line 198

def self.subclass(parent, args, &example_group_block)
  subclass = Class.new(parent)
  subclass.set_it_up(*args)
  subclass.module_eval(&example_group_block) if example_group_block
  subclass
end

.superclass_metadataMetadata

Returns belonging to the parent of a nested [ExampleGroup](ExampleGroup).

Returns:

  • (Metadata)

    belonging to the parent of a nested [ExampleGroup](ExampleGroup)



153
154
155
# File 'lib/rspec/core/example_group.rb', line 153

def self.
  @superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass. : nil
end

.top_level?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/rspec/core/example_group.rb', line 221

def self.top_level?
  @top_level ||= superclass == ExampleGroup
end

.top_level_descriptionObject



396
397
398
# File 'lib/rspec/core/example_group.rb', line 396

def self.top_level_description
  ancestors.last.description
end

.worldObject



27
28
29
# File 'lib/rspec/core/example_group.rb', line 27

def self.world
  RSpec.world
end

Instance Method Details

#described_classObject

Returns the class or module passed to the ‘describe` method (or alias). Returns nil if the subject is not a class or module.

Examples:

describe Thing do
  it "does something" do
    described_class == Thing
  end
end


426
427
428
# File 'lib/rspec/core/example_group.rb', line 426

def described_class
  self.class.described_class
end

#instance_eval_with_rescue(&hook) ⇒ Object

instance_evals the block, capturing and reporting an exception if raised



433
434
435
436
437
438
439
440
# File 'lib/rspec/core/example_group.rb', line 433

def instance_eval_with_rescue(&hook)
  begin
    instance_eval(&hook)
  rescue Exception => e
    raise unless example
    example.set_exception(e)
  end
end

#running_exampleObject

Deprecated.

use [example](ExampleGroup#example-instance_method)



411
412
413
414
# File 'lib/rspec/core/example_group.rb', line 411

def running_example
  RSpec.deprecate("running_example", "example")
  example
end