我目前正在关注作者Michael Hartl的Ruby on Rails教程。我陷入了第10.1.4章,其中测试应该是绿色,但目前是RED,这是错误信息:
Started with run options --seed 48963
FAIL["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.7533403700217605]
test_valid_signup_information_with_account_activation#UsersSignupTest (1.75s)
Failed assertion, no message given.
test/integration/users_signup_test.rb:44:in `block in <class:UsersSignupTest>'
41/41: [=========================================================================================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.78481s
41 tests, 173 assertions, 1 failures, 0 errors, 0 skips
这是包含主要错误(FAIL)的文件,根据报告应该是'断言user.reload.activated?':
require 'test_helper'
class UsersSignupTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
end
test "invalid signup information" do
get signup_path
assert_no_difference 'User.count' do
post users_path, user: { name: "",
email: "user@invalid",
password: "foo",
password_confirmation: "bar" }
end
assert_template 'users/new'
assert_select 'div#error_explanation'
assert_select 'div.alert.alert-danger'
end
test "valid signup information with account activation" do
get signup_path
assert_difference 'User.count', 1 do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" }
end
assert_equal 1, ActionMailer::Base.deliveries.size
user = assigns(:user)
assert_not user.activated?
# Try to log in before activation.
log_in_as(user)
assert_not is_logged_in?
# Invalid activation token
get edit_account_activation_path("invalid token")
assert_not is_logged_in?
get edit_account_activation_path(user.activation_token, email: 'wrong')
assert_not is_logged_in?
get edit_account_activation_path(user.activation_token, email: user.email)
assert user.reload.activated?
follow_redirect!
assert_template 'users/show'
assert is_logged_in?
end
end
-
您还可以在Git Repository
上查看我的存储库如果有人能帮助我解决这个问题,我将不胜感激,这将为我节省宝贵的时间。
答案 0 :(得分:1)
我认为问题归结为这两行:
assert_select 'div#<error_explanation>'
assert_select 'div.<alert alert-danger>'
这些必须是错误中概述的有效CSS选择器。他们不是。固定版本可能是:
assert_select 'div#error_explanation'
assert_select 'div.alert.alert-danger'
这取决于你的风格。记住这些工作就像jQuery一样,所以它们类似于$('div#error_explanation')
。
答案 1 :(得分:0)
您的测试本身看起来很好,但是当您在尝试编辑其帐户激活信息时断言用户实际已被激活时,它们似乎会失败:
get edit_account_activation_path(user.activation_token, email: user.email)
assert user.reload.activated? # <= test fails here
在您的回购邮件的account_activations_controller.rb
文件中,您有:
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate # <= here's where you're activating the user
log_in user
flash[:success] = "Account activated!"
redirect_to user
else
flash[:danger] = "Invalid activation link"
redirect_to root_url
end
end
但是,查看您的user.rb
类,看起来您实际上完全错过了activate
方法(遗憾的是错误消息似乎没有向您表明这一点)。因此,请将以下方法添加到User
模型中,然后再次尝试运行规范:
def activate
update_columns(activated: true, activated_at: Time.current)
end
修改强>
在the point where your tests went red检出您的回购后,我现在可以看到此问题出在您的SessionsHelper#redirect_back_or
方法中:
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete[:forwarding_url]
end
session.delete[:forwarding_url]
应该是session.delete(:forwarding_url)
,括号而不是方括号。