[rspec-users] Help with global before behavior...

Denis Haskin denis at haskinferguson.net
Fri May 8 16:51:06 EDT 2009

Okay, I guess I can do this with shared examples.  I added to my 

describe "an admin user is signed in", :shared => true do
  before( :each ) do
    @admin_user = Factory(:admin_user)
    login_as @admin_user

and then in each controller that needs that behavior:

  it_should_behave_like "an admin user is signed in"

Good enough.  I had myself going in circles because I was trying to use 
a :type and global before behavior, and I think that doesn't work at all 
how I was expecting it to.

Can :type be anything?  Or is it limited to some set of values (e.g. 
:controller, etc)?



Denis Haskin wrote:
> I'm having a devil of a time getting some global before behavior to 
> work the way I think it should.
> I have a set of controllers that are used only by administrative 
> users, so login is required.  To run examples just against the 
> functionality, ideally I'd like to be able to set up a before() that 
> would log an admin user in before each example.
> I tried putting it in as a global in spec_helper.rb:
> Spec::Runner.configure do |config|
> ...etc...
>   config.before(:each) do
>     @admin_user = Factory(:admin_user)
>    login_as @admin_user
>   end
> ...etc...
> but it really seems like it's being executed in the configure context, 
> not the controller context (for example, session[] isn't defined yet, 
> etc).
> Any recommendations?
> Thanks,
> dwh
> ------------------------------------------------------------------------
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list