How to access current_user from Cucumber/Capybara?

2,817 views
Skip to first unread message

AK

unread,
Oct 1, 2010, 10:05:25 AM10/1/10
to Devise
All of my sign-in tests work fine. My tests are explicitly and
successfully logging in. But the Cucumber test page in the Devise wiki
doesn't explain how I can access the "current_user" helper from my
features.

For example, if in my layout I have "You are currently signed in as
UserX". I want to test that view in Cucumber/Capybara.

But I keep getting:

undefined local variable or method `current_user' for
#<Cucumber::Rails::World:0x81d10c50> (NameError)

Carlos Antonio da Silva

unread,
Oct 1, 2010, 10:17:39 AM10/1/10
to plataforma...@googlegroups.com
I believe you should just test the message in flash, not using current_user.name or sth like that.
You're not supposed to access this kind of variables in an integration test.
--
At.
Carlos A. da Silva

AK

unread,
Oct 1, 2010, 11:59:44 AM10/1/10
to Devise
Thank you for the reply Carlos! But I don't know if I have stated this
correctly. And I know I probably have the wrong approach here, as
current_user is probably designed primarily for use in the controller.

At the top of this Google Groups page, I see my name. I want to do
the exact same thing in my secure app, so that if a user is logged in
then that username will be at the top at all times.

Am I better off setting a @user variable in a before_filter in my
ApplicationController, then accessing that variable in the view?


On Oct 1, 10:17 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> I believe you should just test the message in flash, not using
> current_user.name or sth like that.
> You're not supposed to access this kind of variables in an integration test.
>

AK

unread,
Oct 1, 2010, 12:03:39 PM10/1/10
to Devise
Nevermind, that is exactly what I need to do. My thick head needed to
be told "you are not supposed to do that". I spend hours trying to do
it, then when I'm told don't do that I figure it out in seconds.
Thanks again Carlos!

On Oct 1, 10:17 am, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> I believe you should just test the message in flash, not using
> current_user.name or sth like that.
> You're not supposed to access this kind of variables in an integration test.
>

radhames brito

unread,
Oct 1, 2010, 1:19:13 PM10/1/10
to plataforma...@googlegroups.com

Quoting from capy's readme

Gotchas:

  • Access to session and request is not possible from the test, Access to response is limited. Some drivers allow access to response headers and HTTP status code, but this kind of functionality is not provided by some drivers, such as Selenium.

AK

unread,
Oct 1, 2010, 1:50:22 PM10/1/10
to Devise
Thanks Radhames

On Oct 1, 1:19 pm, radhames brito <rbri...@gmail.com> wrote:
> Quoting from capy's readme
> Gotchas:
>
>    - Access to session and request is not possible from the test, Access to
Reply all
Reply to author
Forward
0 new messages