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.
@attr_reader Returns the [Example](Example) object that wraps this instance of `ExampleGroup`
@private
# File lib/rspec/core/example_group.rb, line 382 382: def self.all_apply?(filters) 383: metadata.all_apply?(filters) 384: end
@private
# File lib/rspec/core/example_group.rb, line 216 216: def self.ancestors 217: @_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup} 218: end
@private
# File lib/rspec/core/example_group.rb, line 377 377: def self.any_apply?(filters) 378: metadata.any_apply?(filters) 379: end
@private
# File lib/rspec/core/example_group.rb, line 279 279: def self.assign_before_all_ivars(ivars, example_group_instance) 280: ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) } 281: end
@private
# File lib/rspec/core/example_group.rb, line 266 266: def self.before_all_ivars 267: @before_all_ivars ||= {} 268: end
@private
# File lib/rspec/core/example_group.rb, line 117 117: def self.block_not_supported(label) 118: warn("Customization blocks not supported for include_#{label}. Use it_behaves_like instead.") 119: end
@private
# File lib/rspec/core/example_group.rb, line 206 206: def self.children 207: @children ||= [].extend(Extensions::Ordered) 208: end
@private
# File lib/rspec/core/example_group.rb, line 387 387: def self.declaration_line_numbers 388: @declaration_line_numbers ||= [metadata[:example_group][:line_number]] + 389: examples.collect {|e| e.metadata[:line_number]} + 390: children.inject([]) {|l,c| l + c.declaration_line_numbers} 391: end
@private
# File lib/rspec/core/example_group.rb, line 53 53: def self.define_example_method(name, extra_options={}) 54: module_eval( 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, __FILE__, __LINE__) 55: end
@private
# File lib/rspec/core/example_group.rb, line 38 38: def self.delegate_to_metadata(*names) 39: names.each do |name| 40: define_method name do 41: metadata[:example_group][name] 42: end 43: end 44: end
@private
# File lib/rspec/core/example_group.rb, line 141 141: def self.descendant_filtered_examples 142: @descendant_filtered_examples ||= filtered_examples + children.inject([]){|l,c| l + c.descendant_filtered_examples} 143: end
@private
# File lib/rspec/core/example_group.rb, line 211 211: def self.descendants 212: @_descendants ||= [self] + children.inject([]) {|list, c| list + c.descendants} 213: end
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 DSL#describe
# File lib/rspec/core/example_group.rb, line 178 178: def self.describe(*args, &example_group_block) 179: @_subclass_count ||= 0 180: @_subclass_count += 1 181: args << {} unless args.last.is_a?(Hash) 182: args.last.update(:example_group_block => example_group_block) 183: 184: # TODO 2010-05-05: Because we don't know if const_set is thread-safe 185: child = const_set( 186: "Nested_#{@_subclass_count}", 187: subclass(self, args, &example_group_block) 188: ) 189: children << child 190: child 191: end
@private
# File lib/rspec/core/example_group.rb, line 226 226: def self.ensure_example_groups_are_configured 227: unless defined?(@@example_groups_configured) 228: RSpec.configuration.configure_mock_framework 229: RSpec.configuration.configure_expectation_framework 230: @@example_groups_configured = true 231: end 232: end
@private
# File lib/rspec/core/example_group.rb, line 131 131: def self.examples 132: @examples ||= [] 133: end
@private
# File lib/rspec/core/example_group.rb, line 372 372: def self.fail_fast? 373: RSpec.configuration.fail_fast? 374: end
@private
# File lib/rspec/core/example_group.rb, line 360 360: def self.fail_filtered_examples(exception, reporter) 361: filtered_examples.each { |example| example.fail_with_exception(reporter, exception) } 362: 363: children.each do |child| 364: reporter.example_group_started(child) 365: child.fail_filtered_examples(exception, reporter) 366: reporter.example_group_finished(child) 367: end 368: false 369: end
@private
# File lib/rspec/core/example_group.rb, line 136 136: def self.filtered_examples 137: world.filtered_examples[self] 138: end
Includes shared content declared with `name`.
@see SharedExampleGroup
# File lib/rspec/core/example_group.rb, line 105 105: def self.include_context(name, *args) 106: block_given? ? block_not_supported("context") : find_and_eval_shared("context", name, *args) 107: end
Includes shared content declared with `name`.
@see SharedExampleGroup
# File lib/rspec/core/example_group.rb, line 112 112: def self.include_examples(name, *args) 113: block_given? ? block_not_supported("examples") : find_and_eval_shared("examples", name, *args) 114: end
@private
# File lib/rspec/core/example_group.rb, line 32 32: def self.register 33: world.register(self) 34: end
Runs all the examples in this group
# File lib/rspec/core/example_group.rb, line 326 326: def self.run(reporter) 327: if RSpec.wants_to_quit 328: RSpec.clear_remaining_example_groups if top_level? 329: return 330: end 331: reporter.example_group_started(self) 332: 333: begin 334: run_before_all_hooks(new) 335: result_for_this_group = run_examples(reporter) 336: results_for_descendants = children.ordered.map {|child| child.run(reporter)}.all? 337: result_for_this_group && results_for_descendants 338: rescue Exception => ex 339: fail_filtered_examples(ex, reporter) 340: ensure 341: run_after_all_hooks(new) 342: before_all_ivars.clear 343: reporter.example_group_finished(self) 344: end 345: end
@private
# File lib/rspec/core/example_group.rb, line 307 307: def self.run_after_all_hooks(example_group_instance) 308: return if descendant_filtered_examples.empty? 309: assign_before_all_ivars(before_all_ivars, example_group_instance) 310: 311: begin 312: run_hook(:after, :all, example_group_instance) 313: rescue => e 314: # TODO: come up with a better solution for this. 315: RSpec.configuration.reporter.message An error occurred in an after(:all) hook. #{e.class}: #{e.message} occurred at #{e.backtrace.first} 316: end 317: end
@private
# File lib/rspec/core/example_group.rb, line 302 302: def self.run_after_each_hooks(example) 303: run_hook(:after, :each, example) 304: end
@private
# File lib/rspec/core/example_group.rb, line 292 292: def self.run_around_each_hooks(example, initial_procsy) 293: run_hook(:around, :each, example, initial_procsy) 294: end
@private
# File lib/rspec/core/example_group.rb, line 284 284: def self.run_before_all_hooks(example_group_instance) 285: return if descendant_filtered_examples.empty? 286: assign_before_all_ivars(superclass.before_all_ivars, example_group_instance) 287: run_hook(:before, :all, example_group_instance) 288: store_before_all_ivars(example_group_instance) 289: end
@private
# File lib/rspec/core/example_group.rb, line 297 297: def self.run_before_each_hooks(example) 298: run_hook(:before, :each, example) 299: end
@private
# File lib/rspec/core/example_group.rb, line 348 348: def self.run_examples(reporter) 349: filtered_examples.ordered.map do |example| 350: next if RSpec.wants_to_quit 351: instance = new 352: set_ivars(instance, before_all_ivars) 353: succeeded = example.run(instance, reporter) 354: RSpec.wants_to_quit = true if fail_fast? && !succeeded 355: succeeded 356: end.all? 357: end
@private
# File lib/rspec/core/example_group.rb, line 235 235: def self.set_it_up(*args) 236: # Ruby 1.9 has a bug that can lead to infinite recursion and a 237: # SystemStackError if you include a module in a superclass after 238: # including it in a subclass: https://gist.github.com/845896 239: # To prevent this, we must include any modules in RSpec::Core::ExampleGroup 240: # before users create example groups and have a chance to include 241: # the same module in a subclass of RSpec::Core::ExampleGroup. 242: # So we need to configure example groups here. 243: ensure_example_groups_are_configured 244: 245: symbol_description = args.shift if args.first.is_a?(Symbol) 246: args << build_metadata_hash_from(args) 247: args.unshift(symbol_description) if symbol_description 248: @metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args) 249: world.configure_group(self) 250: [:before, :after, :around].each do |_when| 251: RSpec.configuration.hooks[_when][:each].each do |hook| 252: unless ancestors.any? {|a| a.hooks[_when][:each].include? hook } 253: hooks[_when][:each] << hook # each's get filtered later per example 254: end 255: end 256: next if _when == :around # no around(:all) hooks 257: RSpec.configuration.hooks[_when][:all].each do |hook| 258: unless ancestors.any? {|a| a.hooks[_when][:all].include? hook } 259: hooks[_when][:all] << hook if hook.options_apply?(self) 260: end 261: end 262: end 263: end
@private
# File lib/rspec/core/example_group.rb, line 399 399: def self.set_ivars(instance, ivars) 400: ivars.each {|name, value| instance.instance_variable_set(name, value)} 401: end
@private
# File lib/rspec/core/example_group.rb, line 271 271: def self.store_before_all_ivars(example_group_instance) 272: return if example_group_instance.instance_variables.empty? 273: example_group_instance.instance_variables.each { |ivar| 274: before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar) 275: } 276: end
@private
# File lib/rspec/core/example_group.rb, line 198 198: def self.subclass(parent, args, &example_group_block) 199: subclass = Class.new(parent) 200: subclass.set_it_up(*args) 201: subclass.module_eval(&example_group_block) if example_group_block 202: subclass 203: end
@private @return [Metadata] belonging to the parent of a nested [ExampleGroup](ExampleGroup)
# File lib/rspec/core/example_group.rb, line 153 153: def self.superclass_metadata 154: @superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil 155: end
@private
# File lib/rspec/core/example_group.rb, line 221 221: def self.top_level? 222: @top_level ||= superclass == ExampleGroup 223: end
Returns the class or module passed to the `describe` method (or alias). Returns nil if the subject is not a class or module. @example
describe Thing do it "does something" do described_class == Thing end end
# File lib/rspec/core/example_group.rb, line 424 424: def described_class 425: self.class.described_class 426: end
@private instance_evals the block, capturing and reporting an exception if raised
# File lib/rspec/core/example_group.rb, line 431 431: def instance_eval_with_rescue(&hook) 432: begin 433: instance_eval(&hook) 434: rescue Exception => e 435: raise unless example 436: example.set_exception(e) 437: end 438: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.