Class: RSpec::Core::ExampleGroup

Inherits:
Object
  • Object
show all
Extended by:
Hooks, Subject::ExampleGroupMethods
Includes:
Let, Pending, Subject::ExampleMethods
Defined in:
lib/rspec/core/example_group.rb

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 Subject::ExampleGroupMethods

its, subject

Methods included from Hooks

after, append_after, around, before, prepend_before

Methods included from Pending

#pending

Methods included from Subject::ExampleMethods

#_attribute_chain, #_nested_attribute, #subject

Instance Attribute Details

#exampleObject

Returns the Example object that wraps this instance of ExampleGroup



406
407
408
# File 'lib/rspec/core/example_group.rb', line 406

def example
  @example
end

Class Method Details

.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

.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 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

.run(reporter) ⇒ Object

Runs all the examples in this group



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

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

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


424
425
426
# File 'lib/rspec/core/example_group.rb', line 424

def described_class
  self.class.described_class
end

#running_exampleObject

Deprecated.

use example



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

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