Rails 3.1,工厂女孩错误

发布于 2024-11-27 12:22:08 字数 2346 浏览 0 评论 0原文

固定的。 Rails 中有一个错误。请参阅 https://github.com/rails/rails/issues/2333

我有一个Factory Girl Rails 和 Rails 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 创建任意数量的用户,但只能在 Rails 控制台中创建。

测试:

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]) 导致问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-12-04 12:22:08

不知何故,该类没有正确查找,我不确定这是如何发生的,但您可以尝试以不同的方式访问它:

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

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
余生共白头 2024-12-04 12:22:08

你的 Factory.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

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
泪是无色的血 2024-12-04 12:22:08

这应该有效:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文