im working with railstutorial.org, im currently at 9th chapter, doing exercises. I have to write an integration test for all the layout links, including the proper behavior for logged-in and non-logged-in users. There's an exercise: Write an integration test for all the layout links, including the proper behavior for logged-in and non-logged-in users. Hint: Add to the test in Listing 5.25 using the log_in_as helper.
This is 5.25 Listing:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
end
end
This is the line i came up with to solve the exercise:
assert_select "a[href=?]", login_path || users_path
My tests are green, but did I am not sure if I understood the exercise right, and If i solved it.
So my question is, how do I write the test described in the exercise, thanks
You need to add something like this:
# Not logged in so a "Login" link will be present
assert_select "a[href=?]", login_path
# Login
log_in_as(@user)
# Go to root
get root_path
# Now logged in so a "Login" link won't be present
assert_select "a[href=?]", login_path, count: 0
# Now logged in so a "Logout" link will be present
assert_select "a[href=?]", logout_path
# Now logged in so a "Users" link will be present
assert_select "a[href=?]", users_path
# Now logged in so a "Accounts" link will be present
assert_select 'a[href=?]', '#', text: 'Accounts'
Don't forget to include the following just after class SiteLayoutTest < ActionDispatch::IntegrationTest
:
def setup
@user = users(:michael)
end
I am working through this rails tutorial as well. Here is the full test I came up with. I may have went a bit extreme here, but the exercise does state that all links should be tested both logged in and logged out. This includes the user's profile and Settings links at user_path
and edit_user_path
respectively. One step that helped me was to actually look at the site both as a logged in and non-logged in user and even pull up the source to determine the full list of links as indicated by the a href
tags in the source. Also, make sure you get root_path
after your log_in_as(@user)
. This will save you the confusion and headache that I suffered.
test "layout links for logged-in and non-logged-in users" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", signup_path
assert_select "a[href=?]", login_path
log_in_as(@user)
get root_path
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", users_path
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", "#", text: "Account"
assert_select "a[href=?]", user_path(@user)
assert_select "a[href=?]", edit_user_path(@user)
end
My steps are setup a user, test for layout links before login, use log_in_as
helper to login the user, use follow_redirect!
to actually visit the redirect page, then test for layout links after login.
This is my code:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", signup_path
get signup_path
assert_select "title", full_title("Sign up")
assert_select "a[href=?]", login_path
get login_path
assert_select "title", full_title("Log in")
log_in_as(@user)
assert_redirected_to @user
follow_redirect!
assert_select "h1", @user.name
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
assert_select "a[href=?]", users_path
assert_select "a[href=?]", user_path(@user)
assert_select "a[href=?]", edit_user_path(@user)
assert_select "a[href=?]", logout_path
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.