我正在关注Michal Hartls Rails教程第7章。我已经安装了Factory girl gem。我在运行测试时不断收到此错误
Failures:
1) UsersController GET 'show' should be successful
Failure/Error: @user = FactoryGirl.create(:user)
ArgumentError:
Factory not registered: user
# ./spec/controllers/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 0.66336 seconds 42 examples, 1 failure
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:14 # UsersController GET 'show' should be successful
的Gemfile
group :test do
gem 'autotest', '4.4.6'
gem "autotest-growl", "~> 0.2.16"
gem "autotest-rails", "~> 4.1.2"
gem "rspec", "~> 2.9.0"
gem 'rspec-rails', '2.9.0'
gem 'webrat', '0.7.3'
gem "spork", '0.9.0'
gem 'annotate', '~> 2.4.1.beta'
gem "factory_girl", "~> 3.2.0"
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform =>
我的user_controller_spec.rb是
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
@user = FactoryGirl.create(:user)
end
it "should be successful" do
get :show, :id => @user
response.should be_success
end
end
describe "GET 'new'" do
it "should be successful" do
get :new
response.should be_success
end
it "should have the right title" do
get :new
response.should have_selector('title', :content => "Sign up")
end
end
end
我的个工厂.rb是
FactoryGirl.create :user do |user|
user.name "Mark Brown"
user.email "mbrown@yahoo.com"
user.password "foobar"
user.password_confirmation "foobar"
end
我改变了
Factory(:name)
到
FactoryGirl.create(:user)
在我user_controller_spec.rb
的第10行,因为我收到了一份加油警告。我认为这样可以解决它,但现在我才刚刚开始。
Factory not registered: user
有人可以告诉我发生了什么吗?
答案 0 :(得分:53)
我已经修好了。
解决方案是在我的Gemfile中将gem 'factory_girl'
更改为gem 'factory_girl_rails'
。
答案 1 :(得分:41)
当我第一次在该章中使用FactoryGirl时,我遇到了同样的错误,我需要做的就是重新启动服务器和Spork,如果你正在使用它。希望它有所帮助
答案 2 :(得分:6)
如果先定义工厂,而不是创建工厂怎么办? 试试这个:
FactoryGirl.define do
factory :user do
name "Mark Brown"
email "mbrown@yahoo.com"
password "foobar"
password_confirmation "foobar"
end
end
答案 3 :(得分:3)
您需要测试组
下的宝石'factory_girl_rails'答案 4 :(得分:1)
在你的Gemfile中你应该用这个替换
gem "factory_girl_rails", "~> 4.0"
而不是
gem "factory_girl", "~> 3.2.0"
答案 5 :(得分:0)
我使用pry作为我的IRB。 当我从我的宝石中删除它时,我能够让Factory女孩正常使用。
rails c -e test
irb -> FactoryGirl.create(:user)
也许有人可以解释如何使用pry来解决这个问题。