1

Railsアプリケーションの外でFactoryGirlを書いています。support/factories.rb にファクトリーを作成しました

require 'factory_girl'

FactoryGirl.define do
  factory :user do
    first_name { Faker::Name.first_name }
    last_name { Faker::Name.last_name }
    email { Faker::Internet.email }
    password { "a123456Q" }
    address { Faker::Address.street_name }
    mobile {"1234567890"}
    country {"india"}
    login {Faker::Name.first_name}
  end
end

env.rbで接続しました

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql2',
  :host => 'localhost',
  :database => 'consultancy',
  :username => "root",
  :password => "")

puts ActiveRecord::Base.connection.execute("select * from users")

ここにリターンを置きます<Mysql2::Result:0xa932d80>

そして、コマンドを実行するFactoryGirl.factoriesと戻ります

#<FactoryGirl::Registry:0xa39d118 @name="Factory", @items={:user=>#<FactoryGirl::Factory:0xb2e82d0 @name=:user, @parent=nil, @aliases=[], @class_name=nil, @definition=#<FactoryGirl::Definition:0xb000f68 @declarations=#<FactoryGirl::DeclarationList:0xb000f90 @declarations=[#<FactoryGirl::Declaration::Dynamic:0xa66b098 @name=:first_name, @ignored=false, @block=#<Proc:0xa66b070@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:5>>, #<FactoryGirl::Declaration::Dynamic:0x9ac5f7c @name=:last_name, @ignored=false, @block=#<Proc:0x9ac5cd4@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:6>>, #<FactoryGirl::Declaration::Dynamic:0x9ef23fc @name=:email, @ignored=false, @block=#<Proc:0x9ef2168@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:7>>, #<FactoryGirl::Declaration::Dynamic:0xa737e2c @name=:password, @ignored=false, @block=#<Proc:0xa737760@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:8>>, #<FactoryGirl::Declaration::Dynamic:0xa29a630 @name=:address, @ignored=false, @block=#<Proc:0x9f761e8@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:9>>, #<FactoryGirl::Declaration::Dynamic:0xa145b04 @name=:mobile, @ignored=false, @block=#<Proc:0xa754d74@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:10>>, #<FactoryGirl::Declaration::Dynamic:0xa5d4918 @name=:country, @ignored=false, @block=#<Proc:0xa5d4878@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:11>>, #<FactoryGirl::Declaration::Dynamic:0xa5d4b5c @name=:login, @ignored=false, @block=#<Proc:0xa5d4ae4@/home/vijay_demo_project/bank_cucumber/features/support/factories.rb:12>>], @name=:user, @overridable=false>, @callbacks=[], @defined_traits=[], @to_create=nil, @base_traits=[], @additional_traits=[], @constructor=nil, @attributes=nil, @compiled=false>, @compiled=false>}>

しかし、ビルドを作成するとエラーが発生します。

 FactoryGirl.build(:user)
*** NameError Exception: uninitialized constant User

Railsにはモデルユーザーがあり、データベースにテーブルユーザーが存在します。

私が間違っているところを助けてください。

4

1 に答える 1