Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 21:41:43

It should be FactoryGirl.create instead. Apparently Factory was deprecated and now has been removed, look at the comments in the link you provided :)

In fact in your spec_helper.rb under Rspec.configure do...end you can add

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end 

This will save you the trouble of the prefixing FactoryGirl. before :build and :create altogether:

require 'spec_helper'

describe Admin do
  it "has a valid factory" do
    create(:admin).should be_valid
  end
  it "is invalid without a name"
  it "is invalid without an email"
end

Refer: FactoryGirl Documentation

This isn't an answer to your question, but I noticed there is an obscure error in your use of Faker with FactoryGirl. f.name and f.email will be the same for every FactoryGirl.create or FactoryGirl.build.

f.name Faker::Name.name
f.email Faker::Internet.email

Add curly braces around the Faker calls so that each reference to a Factory will generate random Faker data.

f.name { Faker::Name.name }
f.email { Faker::Internet.email }

Also, be sure that you are including the require statement in your spec_helper.rb file.

require 'factory_girl_rails'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!