2

在 Michael Hartl 教程中的示例 rails 应用程序上运行 bundle exec rspec 时,出现错误“参数错误:工厂未注册:用户”

我已经看到了一堆非常相似的问题,但我已经尝试了我看到的两种解决方案,它们是

a) 重启 rails 服务器 b) 安装“factory_girl_rails”

我的 Gemfile 的相关部分:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'

group :test do
  gem 'selenium-webdriver', '2.0.0'
  gem 'capybara', '2.1.0'   
  gem 'factory_girl_rails', '4.2.1'
end

工厂.rb

#root/spec/factories.rb

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

user_pages_spec.rb

#root/spec/requests/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title('Sign up') }
  end
end

显示.html.erb

#root/app/views/user/show.html.erb

<% provide(:title, @user.name) %>
<h1><%= @user.name %></h1>

错误输出

Failures:

   1) User pages profile page 
      Failure/Error: let(:user) { FactoryGirl.create(:user) }
      ArgumentError:
        Factory not registered: user
      # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
      # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

我以前从未屈服于堆栈溢出之神,但我正要把我的电脑扔出窗外。非常感谢您的帮助!

4

1 回答 1

1

Bigxiang的评论是解决方案。我是初学者,实际上并不知道我的目录名称很重要。一个错字导致了问题。

于 2013-08-23T13:28:55.853 回答