gpt4 book ai didi

ruby-on-rails - railstutorial.org - 未定义的方法 `Factory'

转载 作者:行者123 更新时间:2023-12-03 09:25:47 25 4
gpt4 key购买 nike

我正在尝试关注 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

最佳答案

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

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

关于ruby-on-rails - railstutorial.org - 未定义的方法 `Factory',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3991052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com