rspec 用户测试给出“未定义的局部变量或方法‘confirmed_at’” ”
我的 rspec 测试给了我
NameError:
undefined local variable or method `confirmed_at' for #<User:0xca6ff98>
我的用户规范是:
require 'spec_helper'
describe User do
before(:each) do
@user = Factory(:user)
end
# Factory will make sure that in the future if attributes are added the tests below don't break
# Just as long as the Factory is updated for the new attributes as appropriate.
context "email is null" do
it "record is invalid " do
@user.name = nil
@user.should_not be_valid
end
end
context "email is unique" do
it "record is valid " do
@user2 = Factory(:user)
@user2 = @user.email
@user2.should_not be_valid
end
end
end
我找不到任何对 recognize_at 的引用
我设计了 1.4.8 我的用户迁移是:
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
drop_table :users
end
end
我的用户表是:
mysql> describe users;
+------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | UNI | | |
| encrypted_password | varchar(128) | NO | | | |
| reset_password_token | varchar(255) | YES | UNI | NULL | |
| reset_password_sent_at | datetime | YES | | NULL | |
| remember_created_at | datetime | YES | | NULL | |
| sign_in_count | int(11) | YES | | 0 | |
| current_sign_in_at | datetime | YES | | NULL | |
| last_sign_in_at | datetime | YES | | NULL | |
| current_sign_in_ip | varchar(255) | YES | | NULL | |
| last_sign_in_ip | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| last_name | varchar(255) | YES | | NULL | |
| first_name | varchar(255) | YES | | NULL | |
+------------------------+--------------+------+-----+---------+----------------+
my rspec test is giving me
NameError:
undefined local variable or method `confirmed_at' for #<User:0xca6ff98>
My User spec is:
require 'spec_helper'
describe User do
before(:each) do
@user = Factory(:user)
end
# Factory will make sure that in the future if attributes are added the tests below don't break
# Just as long as the Factory is updated for the new attributes as appropriate.
context "email is null" do
it "record is invalid " do
@user.name = nil
@user.should_not be_valid
end
end
context "email is unique" do
it "record is valid " do
@user2 = Factory(:user)
@user2 = @user.email
@user2.should_not be_valid
end
end
end
I can't find any reference to confirmed_at
I have devise 1.4.8
My user migration was:
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
drop_table :users
end
end
My user table is:
mysql> describe users;
+------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | UNI | | |
| encrypted_password | varchar(128) | NO | | | |
| reset_password_token | varchar(255) | YES | UNI | NULL | |
| reset_password_sent_at | datetime | YES | | NULL | |
| remember_created_at | datetime | YES | | NULL | |
| sign_in_count | int(11) | YES | | 0 | |
| current_sign_in_at | datetime | YES | | NULL | |
| last_sign_in_at | datetime | YES | | NULL | |
| current_sign_in_ip | varchar(255) | YES | | NULL | |
| last_sign_in_ip | varchar(255) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| last_name | varchar(255) | YES | | NULL | |
| first_name | varchar(255) | YES | | NULL | |
+------------------------+--------------+------+-----+---------+----------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您在途中的某个地方错过了 Devise 的“可确认”配置。您可以自己添加三个缺失的列或尝试类似的操作(请参阅 此线程:
然后编辑迁移到此:
最后,通常:
我认为上述过程应该添加您的三列:
confirmed_at :datetime
confirmation_token :string
confirmation_sent_at :datetime
为您服务或者您可以手动完成。
Looks like you missed the "confirmable" configuration for Devise somewhere along the way. You can add the three missing columns yourself or try something like this (see Reza.Hashemi's message near the bottom of this thread:
Then edit the migration to this:
and finally, the usual:
I think the above process should add your three columns:
confirmed_at :datetime
confirmation_token :string
confirmation_sent_at :datetime
for you. Or you can do it by hand.
对于 Mongoid,查看用户模型——所需的字段可能被注释掉。对我来说,他们是:
For Mongoid, look in the user model -- the needed fields may be commented out. For me, they were: