gpt4 book ai didi

ruby-on-rails - Rails 3.1,工厂女孩错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:37 26 4
gpt4 key购买 nike

已修复。 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

工厂.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

模型/用户.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]) 导致问题。

最佳答案

不知何故没有正确查找类,我不确定这是怎么发生的,但你能尝试以不同的方式访问它吗:

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

关于ruby-on-rails - Rails 3.1,工厂女孩错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com