Rails 3.1, Factory girl bug
Asked Answered
R

3

6

Fixed. There was a bug in Rails. See https://github.com/rails/rails/issues/2333

I have a problem with Factory Girl Rails and Rails 3.1.0.rc5

When I do more than once user = FactoryGirl.create(:user) I have an error.

 Failure/Error: user = FactoryGirl.create(:user)
 NameError:
   uninitialized constant User::User
 # ./app/models/user.rb:17:in `generate_token'
 # ./app/models/user.rb:4:in `block in <class:User>'
 # ./spec/requests/users_spec.rb:20:in `block (3 levels) in <top (required)>'

I can create as many user as I want using Factory but only in rails console.

Tests:

require 'spec_helper'

describe "Users" do

  describe "signin" do

    it "should sign in a user" do
      visit root_path
      user = FactoryGirl.create(:user)
      within("div#sign_in_form") do
        fill_in "Name", with: user.name
        fill_in "Password", with: user.password
      end
      click_button "Sign in"
      current_path.should eq(user_path(user))
      page.should have_content("signed in")
    end

    it "should not show new user form on /" do
      user = FactoryGirl.create(:user)
          visit root_path
      page.should_not have_css("div#new_user_form")
    end
  end
end

factories.rb

FactoryGirl.define do
  factory :user do |f|
    f.sequence(:name) { |n| "john#{n}" }
    f.fullname  'Doe'
    f.sequence(:email) { |n| "test#{n}@example.com" }
    f.password 'foobar'
  end
end

model/user.rb

class User < ActiveRecord::Base
  has_secure_password
  attr_accessible :name, :fullname, :email, :password
  before_create { generate_token(:auth_token) }

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :name, presence: true, length: { maximum: 20 },
            uniqueness: { case_sensitive: false }
  validates :fullname, presence: true, length: { maximum: 30 }
  validates :email, format: { with: email_regex },
            uniqueness: { case_sensitive: false }, length: { maximum: 30 }
  validates :password, length: { in: 5..25 }

  def generate_token(column)
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
end

User.exists?(column => self[column]) causes the problem.

Rescue answered 2/8, 2011 at 13:53 Comment(3)
That is weird, and fixing the root problem would be better, but one thing you may want to do (good practice in general) is to create your user in a setup method. It then becomes available for all proceeding tests.Micropathology
Is this fixed now? Please accept an answer...H
There was a bug in rails which caused that. See this github.com/rails/rails/issues/2333 for more information.Rescue
A
3

Somehow the class is not properly looked up, and I am not sure how this is happenning but could you try accessing it differently:

def generate_token(column)
  begin
    self[column] = SecureRandom.urlsafe_base64
  end while self.class.exists?(column => self[column])
end
Absorbefacient answered 16/10, 2012 at 20:29 Comment(0)
M
0

You've got an extra line i your factories.rb, it should read like this:

FactoryGirl.define :user do |f|
  f.sequence(:name) { |n| "john#{n}" }
  f.fullname  'Doe'
  f.sequence(:email) { |n| "test#{n}@example.com" }
  f.password 'foobar'
end
Metropolitan answered 3/8, 2011 at 8:1 Comment(2)
I'm using git version of Factory-girls and your code doesn't work. /vendor/ruby/1.9.1/gems/factory_girl-2.0.1/lib/factory_girl/syntax/default.rb:6:in define': wrong number of arguments (1 for 0) (ArgumentError) See documentation: [link]github.com/thoughtbot/factory_girl/blob/master/…Rescue
try using Factory.define instead of FactoryGirl.define. You might need to try the factory_girl_rails gemMetropolitan
A
0

This should work:

FactoryGirl.define  do
  factory :user do
    sequence(:name) { |n| "john#{n}" }
    fullname  'Doe'
    sequence(:email) { |n| "test#{n}@example.com" }
    password 'foobar'
  end
end
Airstrip answered 8/9, 2011 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.