2011-08-02 102 views
6

固定。 Rails中存在一個錯誤。見https://github.com/rails/rails/issues/2333Rails 3.1,工廠女孩bug

我有一個工廠女孩護欄及護欄3.1.0.rc5

一個問題,當我做不止一次user = FactoryGirl.create(:user)更多我有一個錯誤。

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)>' 

我可以使用Factory創建儘可能多的用戶,但只能在Rail控制檯中創建。

測試:

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 

模型/ 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+\-.][email protected][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])導致該問題。

+0

這很奇怪,修復根本問題將被打賭ter,但是你可能想要做的一件事(一般而言是好的做法)是用'setup'方法創建你的用戶。然後它可用於所有的進行測試。 – MrDanA

+0

現在是否已修復?請接受答案... – WattsInABox

+0

導致這種情況的軌道有一個錯誤。有關更多信息,請參閱https://github.com/rails/rails/issues/2333。 – chg

回答

0

你已經有了一個額外的行我你factories.rb,就應該這樣寫:

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

我使用工廠女孩的git版本,您的代碼不起作用。 '/vendor/ruby/1.9.1/gems/factory_girl-2.0.1/lib/factory_girl/syntax/default.rb:6:in define':錯誤的參數數量(1代表0)(ArgumentError) '參見文檔:[link] https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md – chg

+0

嘗試使用Factory.define而不是FactoryGirl.define。您可能需要嘗試factory_girl_rails gem –

0

這應該工作:

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

不知何故,類不能正常擡頭,我不知道這是如何發生,但你可以嘗試以不同的方式訪問它:

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