2013-06-26 18 views
24

RSpecでモデルをテストしたいのですが、私が見つけられないタイプミスに遭遇した可能性があります。誰か助けてもらえますか?私は長い間それと苦労してきたし、間違いを見つけることはできません。前もって感謝します!工場が登録されていません

user_spec.rb

require 'spec_helper' 

describe User do 
    it "has a valid factory" do 
     FactoryGirl.build(:user).should be_valid 
    end 
    it "is invalid without an e-mail" 
    it "is invalid without a correct e-mail" 
    it "is invalid without a password" 
    it "is invalid without a matching password confrimation" 
end 

user.rb

FactoryGirl.define do 
    factory :user do |f| 
     f.email "[email protected]" 
     f.password "ruby" 
     f.password_confrimation "ruby" 
    end 
end 

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'factory_girl' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 

エラー

Factory not registered: user 
+1

http://stackoverflow.com/questions/4303329/rspec-cant-find-factorys-from-factorygirlをご覧ください。私はあなたがfactory_girl_rails宝石が必要だと思います。 –

+0

私はすでにそれを持っています。 gem list |グレップのRSpecの*出力は RSpecのレール(2.13.2)である – Kert

+0

factory_girl_railsはrspec_rails –

答えて

30

あなたの質問によると、間違ったファイルに工場の定義があります。それはuser.rbにあります。あなたはRSpecの

# user.rb 

FactoryGirl.define do 
    factory :user do |f| 
     f.email "[email protected]" 
     f.password "ruby" 
     f.password_confrimation "ruby" 
    end 
end 

これに上記の変更、(また、あなたはF変数を必要としない)

# spec/factories.rb 

FactoryGirl.define do 
    factory :user do 
     email "[email protected]" 
     password "ruby" 
     password_confrimation "ruby" 
    end 
end 

を使用する場合、これはあなたのテストフォルダ(スペック)にfactories.rbにする必要がありますまた、コメントが言うように、/だけではなくgem 'factory_girl'

+0

私はちょうど私がやったテストに基づいて、factories.rbの命名に関して明らかに間違っていました。あなたがあなたの答えにマイナーな編集をするなら、私は私のdownvoteを削除します。申し訳ありません。 .... –

+4

定義は 'spec/factories/user.rb'に入り、正常に動作します。工場出荷時の定義を分割できなかった場合、 'spec/factories.rb'ファイルは時間の経過とともに大きく膨らむ可能性があります。 –

+0

@PaulFioravanti合意。簡単にするために、「ユーザー」と表示されていないため。rb'は 'spec'フォルダのどこかにありましたが、わかりやすかったと思って' factories.rb'を提案しました。 – fontno

0

私は私のフォルダ工場で構造と、次の問題に遭遇していたの、gem 'factory_girl_rails'があなたのGemfileであることを確認してください - factories/ -- artists.rb -- techniques.rb

artists.rb

FactoryBot.define do 
    factory :artist do 
     name  'Michael' 
     technique FactoryBot.create :technique 
    end 
    end 

techniques.rb

FactoryBot.define do 
    factory :technique do 
    name 'Some name' 
    end 
end 

技術のオブジェクトがロードされる前に、だから、アーティストのロードされました。だからそれを見つけることができませんでした。解決策は、ファクトリでネストされたFactoryBot createを使用しないか、ネストされたファクトリを親ファクトリの前にある名前に変更することです。

テクニカルファクトリを移動しました。 factories.rbに移動し、そこに定義します。問題は解決した。

関連する問題