Defining FactoryGirl for User model of Devise fails
Asked Answered
S

1

6

I've spent the last two hours figuring out what is wrong, but could not find the answer anywhere.

Its my first rails application (except Hartl's tutorial) so the solution might be simple.. I'm using Devise to manage my Users, everything is really ok with it up until now.

Trying to test the User model I defined a factory like this:

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

and the test is:

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

But the last line ( it { should be_valid } ) fails the test.

I've printed the value of user/@user (tried both) and it came out nil. Edit: It's not nil. Its

#<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

What is it that I don't see?

Standup answered 27/8, 2012 at 15:42 Comment(0)
S
4

OK, I finally found the problem. It turns out you have to restart "spork" when ever you make a change to the User model, because it preloads it.

Standup answered 27/8, 2012 at 16:35 Comment(1)
You could also try setting config.cache_classes = false as per guides.rubyonrails.org/configuring.html, in config/environments/test.rb - Should stop you having to restart spork each time you make a change.Menstruation

© 2022 - 2024 — McMap. All rights reserved.