本文介绍了railstutorial.org-未定义的方法“工厂"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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

这是我的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,如果有帮助的话:

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

推荐答案

也许您应该尝试使用新语法(请参见 github工厂的自述文件)

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

这篇关于railstutorial.org-未定义的方法“工厂"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:17