[rspec-devel] [#5663] context_setup

Bob Cotton bob.cotton at rallydev.com
Thu Dec 7 17:15:44 EST 2006


[ Posting comments on rubyforge seems to be broken ]

The patch to the selenium and watir tests shows using class vars to
conditionally do "context_setup" stuff.

This falls down when you use the same class var across multiple
contexts, because context share the same class, the class var is
already defined in the second context.

Is context_setup something that will ever make it into rspec? 

I've hacked up a working example. It executes context_setup and
context_teardown in it's own execution context, then copies any
instance vars from context_setup into each spec's execution context.

I can submit a patch, complete with specs, if you think it could be
included.  Otherwise I won't spend the time to fully spec it.

The heart of the matter looks like this:

module Spec::Runner::Context::InstanceMethods
  def run(reporter, dry_run=false)
    reporter.add_context(@name)

    prepare_execution_context_class

    unless context_setup_block.nil? || dry_run
      execution_context_instance = execution_context(nil)
      execution_context_instance.instance_exec(&context_setup_block)
    end

    specifications.each do |specification|
      specification_execution_context = execution_context(specification)
      specification_execution_context.copy_instance_variables_from(execution_context_instance, [:@spec]) unless context_setup_block.nil?
      specification.run(reporter, setup_block, teardown_block, dry_run, specification_execution_context)
    end

    # What happens when no contect_setup, but a context_teardown?
    unless context_teardown_block.nil? || dry_run
      execution_context_instance.instance_exec(&context_teardown_block) 
    end
  end


Thoughts?

- Bob

-- 
Bob Cotton
Test Architect -- Rally Software -- rallydev.com
http://www.testarchitecture.com/blog


More information about the rspec-devel mailing list