Post vs Post_via_redirect in Rails 4 not behaving in expected way -
i'm stumped in particular part of m. hartl's rails guide (ch 8) integration testing. trying validate user login/logout, here's testing code(not working):
##doesnt work test "should display user logout behavior" login_path assert_template 'sessions/new' assert_select "a[href=?]", signup_path, count: 3 assert_select "a[href=?]", login_path, count: 2 post_via_redirect login_path, session: {email: @user.email, password: "password"} assert_redirected_to @user assert logged_in? assert_select "a[href=?]", signup_path, count: 0 assert_select "a[href=?]", login_path, count: 0 delete logout_path follow_redirect! assert_template 'static_pages/home' assert_not logged_in? assert_select "a[href=?]", login_path, count: 2 assert_select "a[href=?]", signup_path, count: 3 end
however, making these modifications make work:
#works test "should display user logout behavior" login_path assert_template 'sessions/new' assert_select "a[href=?]", signup_path, count: 3 assert_select "a[href=?]", login_path, count: 2 post login_path, session: {email: @user.email, password: "password"} assert_redirected_to @user follow_redirect! assert logged_in? assert_select "a[href=?]", signup_path, count: 0 assert_select "a[href=?]", login_path, count: 0 delete logout_path follow_redirect! assert_template 'static_pages/home' assert_not logged_in? assert_select "a[href=?]", login_path, count: 2 assert_select "a[href=?]", signup_path, count: 3 end
the part fails assert_redirected_to
. have checked via response.body
, gets rendered expected...
essentially difference post
v. post_via_redirect
. looking @ source code of 'via_redirect', believe should work doing following redirects until there none left. in addition, have no further redirects i'm testing for:
def create user = user.find_by email: params[:session][:email] if user && user.authenticate(params[:session][:password].downcase) log_in(user) redirect_to user_path(user.id) else flash.now[:danger] = "invalid username/password combination" render 'new' end end def destroy reset_session redirect_to root_path end
from standpoint, here's questions:
do redirection options lost whenever call
via_redirect
, , testing 'assert_redirected_to @user' fails?is there 1 more jump i'm making when running
via_redirect
? seefollow_redirect!
doing runningget(response.location)
.
many thanks!
i suppose issue within
assert_redirected_to @user
this assertions checks response headers of previous action, fine. if follow redirect previous redirect response gone , have response after redirect.
as integration test there no need check every step of it. because if redirect fails next assertion
assert logged_in?
will fail.
so if using post_via_redirect check applies next page.
Comments
Post a Comment