[rspec-users] RSpecOnRails - setup or context_setup?

Tobias Grimm listaccount at e-tobi.net
Fri Dec 29 17:37:27 EST 2006


While working on a controller spec, I got into this situation:

The controller uses in it's initialize method some configuration options
from a Configuration class (singleton). For testing these options need
to be changed, so I started with:

context "..." do
  controller_name "test"

  setup do
     Configuration.instance.option = "this should be used in the tests"

  specify "..." do

This looks fine, but has one problem: the setup code of the context is
executed AFTER the controller has been instantiated, but
Configuration.instance.option is needed BEFORE the controllers
initialize method is called.

One way to work around this, is to move the setup code to context_setup,
which apparently gets called BEFORE the controller gets instantiated.

In my current situation this works fine, but what if I need to setup
something before every specification in a context that needs to be done
before the controller is initialized?



More information about the rspec-users mailing list