1

我无法在 Michael Hartl 的 Ruby on Rails 教程第 7.1.3 章中通过测试。

这是错误消息:

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

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

Finished in 2.72 seconds
37 examples, 2 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page

这是我的 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_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', text: 'Sign up') }
  end
end

这是我的工厂.rb:

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

这是Gemfile

group :test do
  gem 'capybara', '1.1.2'
  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'
  gem 'factory_girl_rails', '1.4.0'
end

我尝试重新启动服务器,然后运行bundle exec rake db:reset​​, db:test:prepare, db:test:load, db:migrate- 似乎没有任何效果。有任何想法吗?

4

1 回答 1

2

spork如果您遵循 Michael Hartl 的指南,那么您可能guard也在使用。除非重新启动 spork,否则 spork 不会记录工厂中的更新。

如果您正在使用它们,请尝试重新启动sporkguard

于 2013-04-28T19:52:36.017 回答