Forum: RSpec Help with global before behavior...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Denis H. (Guest)
on 2009-05-09 00:42
(Received via mailing list)
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
Denis H. (Guest)
on 2009-05-09 00:54
(Received via mailing list)
Okay, I guess I can do this with shared examples.  I added to my
spec_helper.rb:

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

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)?

Thanks,

dwh
Scott T. (Guest)
on 2009-05-09 05:23
(Received via mailing list)
Denis H. wrote:
> 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).

Here's how I do it w/ Flavorpill:

Spec::Runner.configure do |config|
  Spec::Rails::Example::ControllerExampleGroup.class_eval do
    include SpecHelpers::AdminLoginHelper

    before(:each) do
      setup_action
    end
  end
end

Scott
David C. (Guest)
on 2009-05-09 09:34
(Received via mailing list)
On Fri, May 8, 2009 at 1:51 PM, Denis H. <removed_email_address@domain.invalid>
wrote:
> 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)?

:type is the type of group (model, view, controller, etc). We're going
to be adding some sort of tagging thing eventually where you'd be able
to set something up in config that you could later hook w/ a hash
passed to describe or it, but that won't be for a bit.

Your solution seems like a fine option, given the current constraints.
Another thing you could do is make it a macro, so you could say:

describe "something" do
  logged_in_as :admin do
    it "should behave this way" do
      ...
    end
  end

  logged_in_as :anonymous do
    it "should behave differently" do
      ..
    end
  end
end

Depends on what you find more expressive.

Cheers,
David
This topic is locked and can not be replied to.