ruby-on-rails – 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

我找不到对confirmed_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,:unlock_token,: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    |                |
+------------------------+--------------+------+-----+---------+----------------+

解决方法

看起来你错过了路上某处Devise的“可确认”配置.你可以自己添加三个缺失的列或尝试这样的东西(参见Reza.Hashemi在 this thread底部附近的消息:

$rails g migration addconfirmable

然后编辑迁移到此:

def self.up 
  change_table(:users) do |t| 
    t.confirmable 
  end 
end

最后,通常:

$rake db:migrate

我认为上面的过程应该添加你的三列:

> confirmed_at:datetime
> confirmation_token:string
> confirmation_sent_at:datetime

为了你.或者你可以手工完成.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...