integration testing with devise

85 views
Skip to first unread message

thoughtafter

unread,
Feb 12, 2011, 8:19:33 PM2/12/11
to Devise
I've had no luck being able to use integration testing with devise.
My old authlogic integration test looked something like this:

visit "/someunauthenticatedurl"
assert_select 'a', 'sign in'
assert_select 'a', 'sign up'
click_link 'linktoauthenticatedpage'
assert_equal '/users/sign_in', path

However, devise fails this test with a path equal to
linktoauthenticatedpage. The app works fine but the testing does
not. I found these 2 similar unanswered questions:

http://groups.google.com/group/plataformatec-devise/browse_thread/thread/e38b85085ff90784
http://groups.google.com/group/plataformatec-devise/browse_thread/thread/830342239f30eb56

So clearly I'm not the only one running into this. I'm using webrat,
which may be the issue. Is there some known problem between webrat /
rack / warden? I had been thinking of switching to capybara but it
would be nice if I could get through the migration to devise first.

José Valim

unread,
Feb 13, 2011, 10:12:38 AM2/13/11
to Devise
To sign in, you need to access the sign in page, fill in the fields
with the proper credentials and press the sign in button (as you would
in a regular app). I believe there is more information about it in the
wiki.

On Feb 13, 2:19 am, thoughtafter <thoughtaf...@gmail.com> wrote:
> I've had no luck being able to use integration testing with devise.
> My old authlogic integration test looked something like this:
>
>         visit "/someunauthenticatedurl"
>         assert_select 'a', 'sign in'
>         assert_select 'a', 'sign up'
>         click_link 'linktoauthenticatedpage'
>         assert_equal '/users/sign_in', path
>
> However, devise fails this test with a path equal to
> linktoauthenticatedpage.  The app works fine but the testing does
> not.  I found these 2 similar unanswered questions:
>
> http://groups.google.com/group/plataformatec-devise/browse_thread/thr...http://groups.google.com/group/plataformatec-devise/browse_thread/thr...

thoughtafter

unread,
Feb 13, 2011, 3:57:34 PM2/13/11
to Devise
If I was not clear I am trying to test accessing methods protected by
the authenticate_user! before_filter. I wish to test that when those
actions are accessed from a user who is not logged in that they will
be redirected to the sign_in page where they can enter their
credentials and then redirected to the protected action. This works
for the app but when testing with webrat accessing the protected page
returns a 200 with blank content for some reason.


On Feb 13, 7:12 am, José Valim <jose.va...@gmail.com> wrote:
> To sign in, you need to access the sign in page, fill in the fields
> with the proper credentials and press the sign in button (as you would
> in a regular app). I believe there is more information about it in the
> wiki.
>
> On Feb 13, 2:19 am, thoughtafter <thoughtaf...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I've had no luck being able to use integration testing with devise.
> > My old authlogic integration test looked something like this:
>
> >         visit "/someunauthenticatedurl"
> >         assert_select 'a', 'sign in'
> >         assert_select 'a', 'sign up'
> >         click_link 'linktoauthenticatedpage'
> >         assert_equal '/users/sign_in', path
>
> > However, devise fails this test with a path equal to
> > linktoauthenticatedpage.  The app works fine but the testing does
> > not.  I found these 2 similar unanswered questions:
>
> >http://groups.google.com/group/plataformatec-devise/browse_thread/thr......

paul h

unread,
Feb 14, 2011, 5:15:38 PM2/14/11
to Devise
It's definitely a webrat issue with the redirects. They appear to have
a fix, but haven't yet released it:

https://github.com/brynary/webrat/pull/46

I was banging my head against the brick wall yesterday, then I found
the above link, and it looks like they posted a fix just after I
decided to put it on the back burner for the day. After a bit of
googling, I came up with a fix for redirections after sign up, but
signing in has been problem - my code is good, but my tests fail.

This is the fix they posted yesterday for cucumber:

In features/support/webrat_host_fix.rb

Webrat::Session.class_eval do
def current_host
URI.parse(current_url).host || @custom_headers["Host"] ||
default_current_host
end

def default_current_host
adapter.class == Webrat::RackAdapter ? "example.org" :
"www.example.com"
end
end

I ad cobbled together a few different solutions, but having just
dropped that file in, it appears to have immediately fixed the
redirect after sign up. If the above on it's own isn't enough for you,
post back and I'll detail what I did leading up to this point.

(If I hadn't seen you're question, I wouldn't have gone back to the
github page so soon, and wouldn't haven't have found the patch :) )

Hopefully, they'll release the full fix release soon....

Paul

thoughtafter

unread,
Feb 27, 2011, 9:00:39 PM2/27/11
to Devise
Yes, the problem was webrat. I didn't have many tests so I converted
them to use capybara instead of webrat.
Reply all
Reply to author
Forward
0 new messages