30

Michal Hartls Rails チュートリアルの第 7 章に従っています。Factory girl gem をインストールしました。テストを実行すると、このエラーが発生し続けます

Failures:

 1) UsersController GET 'show' should be successful
     Failure/Error: @user = FactoryGirl.create(:user)
     ArgumentError:
       Factory not registered: user
     # ./spec/controllers/users_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.66336 seconds 42 examples, 1 failure

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:14 # UsersController GET 'show' should be successful

Gemfile

group :test do
  gem 'autotest', '4.4.6'
  gem "autotest-growl", "~> 0.2.16"
  gem "autotest-rails", "~> 4.1.2"
  gem "rspec", "~> 2.9.0"
  gem 'rspec-rails',  '2.9.0'
  gem 'webrat', '0.7.3'
  gem "spork",  '0.9.0'
  gem 'annotate', '~> 2.4.1.beta'
  gem "factory_girl", "~> 3.2.0"
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform =>

私の user_controller_spec.rb は

require 'spec_helper'

describe UsersController do

  render_views

  describe "GET 'show'" do
    before(:each) do
    @user = FactoryGirl.create(:user)
    end

    it "should be successful" do
      get :show, :id => @user
      response.should be_success
    end
  end

  describe "GET 'new'" do
    it "should be successful" do
      get :new
      response.should be_success
    end

    it "should have the right title" do
      get :new
      response.should have_selector('title', :content => "Sign up")
    end
  end
end

私の factory.rb は

FactoryGirl.create :user do |user|
  user.name                   "Mark Brown"
  user.email                 "mbrown@yahoo.com"
  user.password               "foobar"
  user.password_confirmation  "foobar"
end

私が変更され

Factory(:name)

FactoryGirl.create(:user)

10行目で、user_controller_spec.rb減価償却の警告が表示されました。私はそれを修正すると思っていましたが、今はちょうど得ています。

Factory not registered: user

誰かが私に何が起こっているのか教えてもらえますか?

4

6 に答える 6

54

私はすでにそれを修正しました。

解決策は、Gemfileに変更gem 'factory_girl'することでした。gem 'factory_girl_rails'

于 2012-11-12T00:01:02.973 に答える
41

その章で初めて FactoryGirl を使用したときに同じエラーが発生し、サーバーと Spork (使用している場合) を再起動するだけで済みました。それが役に立てば幸い

于 2012-06-14T06:04:06.767 に答える
6

factory を作成するのではなく、最初に factory を定義するとどうなりますか? これを試して:

FactoryGirl.define do
  factory :user do
    name                  "Mark Brown"
    email                 "mbrown@yahoo.com"
    password              "foobar"
    password_confirmation "foobar"
  end 
end
于 2012-05-07T14:35:39.783 に答える
3

test グループの下に gem 'factory_girl_rails' が必要です

于 2012-05-16T21:07:54.163 に答える
1

Gemfile では、これに置き換える必要があります

gem "factory_girl_rails", "~> 4.0"

それ以外の

gem "factory_girl", "~> 3.2.0"
于 2014-07-15T03:15:02.890 に答える