Railstutorial.org - 未定义的方法“Factory”

发布于 2024-09-28 05:07:20 字数 1927 浏览 5 评论 0原文

我正在尝试关注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                 "[email protected]"
  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

I'm attempting to follow railstutorial.org, and am currently on Chapter 7, where you start using factories: http://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories

I'm using Rails 3.0.1 and ruby-1.9.2-p0

I can't for the life of me get my rspec tests to pass though, the error i get is

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)>'

my factories.rb looks like this:

# 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                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

and this is my users_controller_spec.rb file:

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

here is my Gemfile, if it helps:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

離人涙 2024-10-05 05:07:20

根据Factory Girl最新版本(当前v4.0.0)
重写factories.rb,

FactoryGirl.define do 
  factory :user do
    name                  "Michael Hartl"
    email                 "[email protected]"
    password              "foobar"
    password_confirmation "foobar"
  end
end

然后从您的用户控制器规范中调用它,如下所示:

FactoryGirl.create(:user)

As per the latest version of Factory Girl (currently v4.0.0)
rewrite factories.rb

FactoryGirl.define do 
  factory :user do
    name                  "Michael Hartl"
    email                 "[email protected]"
    password              "foobar"
    password_confirmation "foobar"
  end
end

then call it from your users controller specs as:

FactoryGirl.create(:user)
无声静候 2024-10-05 05:07:20

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

I got this exact same error message. I just restarted my Spork server and Autotest and everything went green for me.

怪异←思 2024-10-05 05:07:20

也许你应该尝试新的语法(请参阅 Factory Girl 的 github readme

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Maybe you should try the new syntax (see github readme of factory girl)

FactoryGirl.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "[email protected]"
  user.password              "foobar"
  user.password_confirmation "foobar"
end
分開簡單 2024-10-05 05:07:20

在您的规范中使用

  @user = FactoryGirl(:user)

而不是

  @user = Factory(:user)

In your spec use

  @user = FactoryGirl(:user)

instead of

  @user = Factory(:user)
萌吟 2024-10-05 05:07:20

我遇到了这个问题,但这是因为我把工厂女孩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

I had this problem, but it was because I had placed the factory girl gem under the development section instead of the test section of the Gemfile. Once under the test section, it worked. One difference I note between my entry and yours is that mine specifies 1.0:

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'factory_girl_rails', '1.0'
end
栖竹 2024-10-05 05:07:20

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

For me I had to add require 'factory_girl' to test_helper.rb

天邊彩虹 2024-10-05 05:07:20

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

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

My solution: I've accidentally included it in the :development block, and simply had to move it to the :test block

(I've listed it here, because it might help someone who doesn't follow the tutorial correctly)

压抑⊿情绪 2024-10-05 05:07:20

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

@user = FactoryGirl.create(:user)

I have done so,
add require 'factory_girl' to test_helper.rb and

@user = FactoryGirl.create(:user)
无畏 2024-10-05 05:07:20

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

“我们正在将factory_girl重命名为factory_bot(并将factory_girl_rails重命名为
工厂_机器人_rails)。现在与factory_girl 具有相同的功能
以不同的名称。”

更多详细信息请参见:

https://robots.thoughtbot.com/factory_bot

For those finding this page now: note where you once used "FactoryGirl" you must now use "FactoryBot" in your tests. From the thoughtbot announcement page:

"We’re renaming factory_girl to factory_bot (and factory_girl_rails to
factory_bot_rails). All the same functionality of factory_girl, now
under a different name."

More details here:

https://robots.thoughtbot.com/factory_bot

梦屿孤独相伴 2024-10-05 05:07:20

我决定使用最新版本的 Factory Girl,所以我尝试调整代码。对我来说不起作用,所以我

gem 'factory_girl_rails', '1.0'

在 Gemfile 中使用将版本锁定在 1.0

bundle update

重新启动 spork 和自动测试,它起作用了。

I was determined to use the newest version of Factory Girl, so I tried to adapt the code. Didn't work for me, so I used

gem 'factory_girl_rails', '1.0'

in the Gemfile to lock the version at 1.0

bundle update

restart spork and autotest and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文