2011-08-02 27 views
6

Đã sửa lỗi. Có một lỗi trong Rails. Xem https://github.com/rails/rails/issues/2333Rails 3.1, lỗi nhà máy cô gái

Tôi có một vấn đề với Factory Girl Rails và Rails 3.1.0.rc5

Khi tôi làm nhiều hơn một lần user = FactoryGirl.create(:user) Tôi có một lỗi.

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

Tôi có thể tạo bao nhiêu người dùng theo ý muốn khi sử dụng Nhà máy nhưng chỉ trong bảng điều khiển đường ray.

Các xét nghiệm:

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 

mô hình/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]) gây ra vấn đề.

+0

Điều đó thật kỳ lạ và khắc phục sự cố gốc sẽ đặt cược nhưng một điều bạn có thể muốn làm (thực hành tốt nói chung) là tạo người dùng của bạn theo phương thức 'setup'. Sau đó nó trở nên có sẵn cho tất cả các bài kiểm tra tiến hành. – MrDanA

+0

Điều này đã được sửa chưa? Xin vui lòng chấp nhận một câu trả lời ... – WattsInABox

+0

Có một lỗi trong đường ray gây ra điều đó. Xem https://github.com/rails/rails/issues/2333 này để biết thêm thông tin. – chg

Trả lời

0

Bạn đã có thêm một dòng i factories.rb của bạn, nó nên đọc như thế này:

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

Tôi đang sử dụng phiên bản git của Factory-girls và mã của bạn không hoạt động. '/vendor/ruby/1.9.1/gems/factory_girl-2.0.1/lib/factory_girl/syntax/default.rb:6:in xác định ': sai số đối số (1 cho 0) (ArgumentError) ' Xem tài liệu : [link] https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md – chg

+0

thử sử dụng Factory.define thay vì FactoryGirl.define. Bạn có thể cần phải thử gem factory_girl_rails –

0

này nên làm việc:

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

Bằng cách nào đó lớp không đúng cách nhìn lên, và tôi không chắc chắn điều này xảy ra như thế nào nhưng bạn có thể thử truy cập nó theo cách khác không:

def generate_token(column) 
    begin 
    self[column] = SecureRandom.urlsafe_base64 
    end while self.class.exists?(column => self[column]) 
end 
Các vấn đề liên quan