10教程并提出了这个错误
ERROR["test_password_resets", PasswordResetsTest, 2016-04-14 20:33:23 +0800]
test_password_resets#PasswordResetsTest (1460637203.22s)
NoMethodError: NoMethodError: undefined method `reset_sent_at=' for #<User:0x007f942d13d620>
app/models/user.rb:56:in `create_reset_digest'
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:18:in `block in <class:PasswordResetsTest>'
app/models/user.rb:56:in `create_reset_digest'
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:18:in `block in <class:PasswordResetsTest>'
33/33: [=================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.61023s
33 tests, 143 assertions, 0 failures, 1 errors, 0 skips
这是我的用户模型
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
...
#set the password reset attributes
def create_reset_digest
self.reset_token = User.new_token
self.update_attribute(:reset_digest, User.digest(reset_token))
self.update_attribute(:reset_sent_at, Time.zone.now)
end
这是我的控制器代码
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instruction"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
最后,这是我的考验!
class PasswordResetsTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
@user = users(:michael)
end
test "password resets" do
get new_password_reset_path
assert_template 'password_resets/new'
# Invalid email
post password_resets_path, password_reset: { email: "" }
assert_not flash.empty?
assert_template 'password_resets/new'
# Valid email
post password_resets_path, password_reset: { email: @user.email }
assert_not_equal @user.reset_digest, @user.reload.reset_digest
assert_equal 1, ActionMailer::Base.deliveries.size
assert_not flash.empty?
assert_redirected_to root_url
# Password reset form
user = assigns(:user)
# Wrong Email
get edit_password_reset_path(user.reset_token, email: "")
assert_redirected_to root_url
# Inactive user
user.toggle!(:activated)
get edit_password_reset_path(user.reset_token, email: user.email)
assert_redirected_to root_url
user.toggle!(:activated)
# Right email, wrong token
get edit_password_reset_path('wrong token', email: user.email)
assert_redirected_to root_url
# Right email Right token
get edit_password_reset_path(user.reset_token, email: user.email)
assert_template 'password_resets/edit'
assert_select "input[name=email][type=hidden][value=?]", user.email
# Invalid password & confirmation
patch password_reset_path(user.reset_token),
email: user.email,
user: { password: "foobaz", password_confirmation: "barquux" }
assert_select 'div#error_explanation'
# Blank password & confirmation
patch password_reset_path(user.reset_token),
email: user.email,
user: { password: " ", password_confirmation: "foobar" }
assert_not flash.empty?
assert_template 'password_resets/edit'
# valid password & confirmation
patch password_reset_path(user.reset_token),
email: user.email,
user: { password: "foobaz", password_confirmation: "foobaz" }
assert is_logged_in?
assert_not flash.empty?
assert_redirected_to user
end
end
我的应用程序本身运行正常。所以我不确定我是否将测试写错了或者我的应用程序中的某些内容是错误的。请帮忙!
答案 0 :(得分:0)
如果您的应用程序本身工作正常,并且只有测试中断,那么有一件事我可以猜到是错误:
reset_sent_at
环境中无法使用test
属性。
要解决此问题,请尝试运行test
环境的迁移,如下所示:
RAILS_ENV=test rake db:migrate
这很容易就是你唯一需要做的事情。