10

我正在尝试关注 railstutorial.org,目前在第 7 章,您开始使用工厂:http ://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories

我正在使用 Rails 3.0.1 和 ruby​​-1.9.2-p0

我无法通过我的 rspec 测试,但我得到的错误是

Failures:
    1) UsersController GET 'show' should be successful
     Failure/Error: @user = Factory(:user)
     undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x00000101cc5608>
 # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

我的 factory.rb 看起来像这样:

# By using the symbol ':user', we get Factory Girl to simulate the User model.
Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "mhartl@example.com"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

这是我的users_controller_spec.rb文件:

require 'spec_helper'

describe UsersController do
  render_views

  describe "GET 'show'" do
    before(:each) do
      @user = Factory(:user)
    end
    it "should be successful" do
      get :show, :id => @user
      response.should be_success
    end

这是我的 Gemfile,如果有帮助的话:

source 'http://rubygems.org'

gem 'rails', '3.0.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'gravatar_image_tag'

group :development do
  gem 'rspec-rails'
  gem 'annotate-models'
end

group :test do
  gem 'rspec'
  gem 'webrat'
  gem 'spork'
  gem 'factory_girl_rails'
end
4

10 回答 10

21

根据最新版 Factory Girl(目前为 v4.0.0)重写 factory.rb

FactoryGirl.define do 
  factory :user do
    name                  "Michael Hartl"
    email                 "mhartl@example.com"
    password              "foobar"
    password_confirmation "foobar"
  end
end

然后从您的用户控制器规范中调用它:

FactoryGirl.create(:user)
于 2012-09-01T23:47:58.867 回答
12

我收到了完全相同的错误消息。我刚刚重新启动了我的 Spork 服务器和 Autotest,对我来说一切都变绿了。

于 2011-01-02T06:37:47.033 回答
7

也许你应该尝试新的语法(参见工厂女孩的github 自述文件)

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "mhartl@example.com"
  user.password              "foobar"
  user.password_confirmation "foobar"
end
于 2010-10-21T19:42:17.453 回答
3

在您的规范中使用

  @user = FactoryGirl(:user)

代替

  @user = Factory(:user)
于 2012-11-05T03:01:43.943 回答
2

我有这个问题,但这是因为我将工厂女孩 gem 放在了开发部分而不是 Gemfile 的测试部分。一旦在测试部分下,它就起作用了。我注意到我的条目和你的条目之间的一个区别是我的条目指定了 1.0:

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'factory_girl_rails', '1.0'
end
于 2012-01-21T19:37:10.287 回答
0

对我来说,我必须require 'factory_girl'添加test_helper.rb

于 2011-11-07T16:19:45.503 回答
0

我的解决方案:我不小心将它包含在:development块中,只需将其移动到:test块中

(我在这里列出了它,因为它可能会帮助那些没有正确遵循教程的人)

于 2012-03-03T20:35:36.770 回答
0

我已经这样做了,添加require 'factory_girl'test_helper.rb

@user = FactoryGirl.create(:user)
于 2014-01-15T12:19:16.273 回答
0

对于那些现在找到此页面的人:请注意您曾经在哪里使用过“FactoryGirl”,您现在必须在测试中使用“FactoryBot”。从thinkbot公告页面:

“我们将 factory_girl 重命名为 factory_bot(并将 factory_girl_rails 重命名为 factory_bot_rails)。与 factory_girl 的所有功能相同,但现在使用了不同的名称。”

更多细节在这里:

https://robots.thoughtbot.com/factory_bot

于 2018-05-28T15:22:47.787 回答
-1

我决定使用最新版本的 Factory Girl,所以我尝试修改代码。不适合我,所以我用

gem 'factory_girl_rails', '1.0'

在 Gemfile 中将版本锁定为 1.0

bundle update

重新启动 spork 和自动测试,它工作。

于 2012-08-19T02:13:29.163 回答