2012-08-27 9 views
6

Tôi đã dành hai giờ cuối cùng để tìm ra điều gì sai, nhưng không thể tìm thấy câu trả lời ở bất cứ đâu.Xác định FactoryGirl cho mô hình người dùng của Devise không

Ứng dụng đường ray đầu tiên của tôi (ngoại trừ hướng dẫn của Hartl) để giải pháp có thể đơn giản .. Tôi đang sử dụng Devise để quản lý Người dùng của tôi, mọi thứ thực sự ổn với nó cho đến bây giờ.

Đang cố gắng để kiểm tra các mô hình tài khoản tôi xác định một nhà máy như thế này:

FactoryGirl.define do 
    factory :user do 
    email "[email protected]" 
    password "123123" 
    password_confirmation { "123123" } 
    end 
end 

và thử nghiệm là:

describe User do 
# pending "add some examples to (or delete) #{__FILE__}" 

    @user = FactoryGirl.create(:user) 


    subject(:user) 

    it { should respond_to(:email) } 
    it { should respond_to(:password) } 

    it { should be_valid } 
end 

Nhưng dòng cuối cùng (nó {nên be_valid}) không kiểm tra .

Tôi đã in giá trị của người sử dụng/@ người dùng (thử cả hai) và nó được đưa ra con số không. Chỉnh sửa: Không phải là không.

#<User id: 13, email: "[email protected]", encrypted_password: "$2a$04$.lWs6yadJu/Ya67xi.W1F.fd6sWLGkzc/59.lgTi0sA7...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-08-27 15:48:23", updated_at: "2012-08-27 15:48:23"> 

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    # attr_accessible :title, :body 

    validates :email, :presence => true 
    validates :password, :presence => true 
end 

Tôi không thấy gì?

Trả lời

4

OK, cuối cùng tôi đã tìm thấy sự cố. Hóa ra bạn phải khởi động lại "spork" khi bạn thay đổi mô hình User, bởi vì nó tải trước nó.

+4

Bạn cũng có thể thử đặt 'config.cache_classes = false' theo http://guides.rubyonrails.org/configuring.html, trong ** config/environment/test.rb ** - Nên dừng bạn phải khởi động lại spork mỗi khi bạn thực hiện một thay đổi. – veritas1