2011-10-22 8 views
5

kiểm tra rspec của tôi là đem lại cho tôidùng rspec kiểm tra cho "biến địa phương không xác định hoặc phương pháp 'confirmed_at'

NameError: 
    undefined local variable or method `confirmed_at' for #<User:0xca6ff98> 

My tài spec là:

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 

tôi không thể tìm thấy bất kỳ tài liệu tham khảo để confirmed_at

tôi có đưa ra 1.4.8 di cư sử dụng của tôi là:

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 

bảng người dùng của tôi là:

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 |    | 
+------------------------+--------------+------+-----+---------+----------------+ 
+1

Devise có yêu cầu 'confirm_at' trong người dùng của bạn không? Tôi đã không bao giờ sử dụng nó vì vậy đó chỉ là một đoán hoang dã. –

+0

Cảm ơn mu! Có vẻ như vậy nhưng có vẻ lạ rằng máy phát điện của nó không bao gồm nó và trước khi tôi đi và thêm nó bằng tay tôi muốn xem nếu có ai khác đã nhìn thấy điều này. –

+0

Thiếu di chuyển có lẽ? http://groups.google.com/group/plataformatec-devise/browse_thread/thread/f7d79b0cdada6992 –

Trả lời

7

Hình như bạn bỏ lỡ "confirmable" cấu hình cho lập mưu ở đâu đó trên đường đi. Bạn có thể thêm ba cột còn thiếu cho mình hoặc thử một cái gì đó như thế này (xem thông điệp Reza.Hashemi của gần cuối this thread:

$ rails g migration addconfirmable 

Sau đó chỉnh sửa chuyển đổi sang này:

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

và cuối cùng, thông thường:

$ rake db:migrate 

tôi nghĩ rằng quá trình trên nên thêm ba cột của bạn:

  • confirmed_at :datetime
  • confirmation_token :string
  • confirmation_sent_at :datetime

cho bạn. Hoặc bạn có thể làm điều đó bằng tay.

+0

Tuyệt vời mu, thx, bạn là người giỏi nhất! –

+0

Có cơ hội nhận được câu trả lời này được cập nhật cho Devise v3 ??? : D – Jeff

+0

@ Jeff: Xin lỗi, tôi không sử dụng Devise ngay bây giờ vì vậy tôi không biết những gì v3 sẽ cần. –

0

Đối với Mongoid, hãy tìm trong mô hình người dùng - các trường cần thiết có thể được nhận xét. Đối với tôi, họ là:

## Confirmable 
field :confirmation_token, :type => String 
field :confirmed_at,   :type => Time 
field :confirmation_sent_at, :type => Time 
field :unconfirmed_email, :type => String # Only if using reconfirmable