Michael Hartl 在 Rails 教程 ch 8.3 上的 Ruby |不确定为什么这个测试失败
Michael Hartl's Ruby on Rails Tutorial ch 8.3 | Not sure why this test is failing
我正在尝试在此 RoR 应用程序中测试登录和注销。我的测试是这样的:
test "login with valid information followed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_redirected_to root_url
assert_not is_logged_in? # <-- this line is the one that cocks up.
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
标记的行是唯一失败的行,我不确定为什么。跟书上的一模一样
向注销路径发送删除请求路由到此函数:
def destroy
logout
redirect_to root_url
end
注销函数是这样的:
def logout
session.delete(:user_id)
@current_user = nil
end
还有is_logged_in?功能是这样的:
def is_logged_in?
!current_user.nil?
end
所有这 3 个都与书中所说的完全一致,包括名称。 (我确实有自己更好的名字,但我无奈之下改了它们。)
该站点似乎工作正常。 Here's 一个 link 所以你可以自己看看。我可以正常登录和退出。
我在别处使用过 assert_not,效果很好。例如:
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path, session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
本次测试成功。 ('Flash' 指的是页面顶部的一个小框,在这种情况下,告诉用户他们的登录信息无效。)
有人有什么想法吗?非常感谢。
到目前为止,在本教程中,您应该已经使用了会话,我可以看到您在 logout
方法中删除了一个会话。
此外,"helper methods aren't available in tests, we can't use the current_user
as in Listing 8.15, but the session
method is available"
您的 is_logged_in?
方法应该如下所示:
def is logged_in?
!session[:user_id].nil?
end
我正在尝试在此 RoR 应用程序中测试登录和注销。我的测试是这样的:
test "login with valid information followed by logout" do
get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_redirected_to root_url
assert_not is_logged_in? # <-- this line is the one that cocks up.
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
标记的行是唯一失败的行,我不确定为什么。跟书上的一模一样
向注销路径发送删除请求路由到此函数:
def destroy
logout
redirect_to root_url
end
注销函数是这样的:
def logout
session.delete(:user_id)
@current_user = nil
end
还有is_logged_in?功能是这样的:
def is_logged_in?
!current_user.nil?
end
所有这 3 个都与书中所说的完全一致,包括名称。 (我确实有自己更好的名字,但我无奈之下改了它们。)
该站点似乎工作正常。 Here's 一个 link 所以你可以自己看看。我可以正常登录和退出。
我在别处使用过 assert_not,效果很好。例如:
test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path, session: { email: "", password: "" }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
本次测试成功。 ('Flash' 指的是页面顶部的一个小框,在这种情况下,告诉用户他们的登录信息无效。)
有人有什么想法吗?非常感谢。
到目前为止,在本教程中,您应该已经使用了会话,我可以看到您在 logout
方法中删除了一个会话。
此外,"helper methods aren't available in tests, we can't use the current_user
as in Listing 8.15, but the session
method is available"
您的 is_logged_in?
方法应该如下所示:
def is logged_in?
!session[:user_id].nil?
end