Rails 3.2,RSpec,Factory Girl:NameError:未初始化的常量Factory [英] Rails 3.2, RSpec, Factory Girl : NameError: uninitialized constant Factory
问题描述
我一直在关注此 Rails测试入门和我遇到了一个我似乎找不到解决方法的问题.我对Rails非常熟悉,但这是我第一次尝试测试.
Ive been following this introductory to Rails testing and Ive run into an issue I cant seem to find the solution to. Im very familiar with Rails but this is my first foray into testing.
无论如何,当我尝试运行rspec spec/models/admin_spec.rb
时,我有一个非常基本的模型测试,甚至没有完全实现.我在Admin has a valid factory
行中收到以下错误(下面是完整代码)
Anyhow, I have a very basic model test, not even fully implemented and when I try and run rspec spec/models/admin_spec.rb
. I get the following error in the Admin has a valid factory
line (full code below)
Admin has a valid factory
Failure/Error: Factory.create(:admin).should be_valid
NameError:
uninitialized constant Factory
# ./spec/models/admin_spec.rb:6:in `block (2 levels) in <top (required)>'
我假设出于某种原因未加载FactoryGirl,但我印象中应该自动加载它.以下是我的Gemfile,/spec/models/admin_spec.rb和/spec/factories/admins.rb
I assume FactoryGirl isnt being loaded for some reason but I was under the impression it should be automatically loaded. Below is the full code from my Gemfile, /spec/models/admin_spec.rb and /spec/factories/admins.rb
非常感谢您的帮助
宝石文件
source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'mysql2'
gem 'jquery-rails'
gem 'haml'
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'bootstrap-sass', '~> 2.0.2'
gem 'capistrano'
gem 'json'
gem "paperclip", '~>3.0'
gem 'airbrake'
gem 'acts_as_list'
gem 'nested_form', :git => 'https://github.com/ryanb/nested_form.git'
gem 'bootstrap-wysihtml5-rails'
gem 'will_paginate', '~> 3.0'
gem 'bootstrap-will_paginate'
gem 'thinking-sphinx', '2.0.10'
gem 'sass-rails', '~> 3.1'
gem 'coffee-rails'
gem 'uglifier'
# gem 'compass'
group :development do
gem 'awesome_print'
gem 'wirble'
end
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
group :production do
gem 'execjs'
gem 'therubyracer'
end
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'launchy'
end
/spec/factories/admin.rb
require 'faker'
FactoryGirl.define do
factory :admin do |f|
f.name Faker::Name.name
f.email Faker::Internet.email
end
end
/spec/models/admin_spec.rb
require 'spec_helper'
describe Admin do
it "has a valid factory" do
Factory.create(:admin).should be_valid
end
it "is invalid without a name"
it "is invalid without an email"
end
推荐答案
应改为FactoryGirl.create
.显然,Factory
已过时,现在已被删除,请查看您提供的链接中的注释:)
It should be FactoryGirl.create
instead. Apparently Factory
was deprecated and now has been removed, look at the comments in the link you provided :)
这篇关于Rails 3.2,RSpec,Factory Girl:NameError:未初始化的常量Factory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!