2013-08-26 26 views
0

我有一些測試,隨機失敗,約。 20%的時間。這意味着在不更改代碼的情況下,每次運行測試時,5次中的1次都會失敗,並顯示「工廠未註冊」錯誤。這是很奇怪.. :(FactoryGirls隨機出現'Factory not registered',爲什麼?

這是consone輸出:

Failures: 

    1) Unit#new_from_string returns factor for metric conversions 
    Failure/Error: FactoryGirl.create :taza 
    ArgumentError: 
     Factory not registered: taza 
    # ./spec/models/unit_spec.rb:29:in `block (2 levels) in <top (required)>' 

Finished in 0.29619 seconds 
4 examples, 1 failure 

Failed examples: 

rspec ./spec/models/unit_spec.rb:22 # Unit#new_from_string returns factor for metric conversions 

Randomized with seed 61727 

這是代碼:

文件: 「unit_spec.rb」

require 'spec_helper' 

describe Unit, "#new_from_string" do 
    it "parses the string and returns a Unit object" do 
    [some tests...] 

    FactoryGirl.find_definitions 
    u = FactoryGirl.create :taza 
    FactoryGirl.create :tbsp 

    [tests...] 
    end 

    it "returns factor for metric conversions" do 
    [tests not involving factory girl...] 

    # the following is line 29, that fails 
    FactoryGirl.create :taza 

    [tests...] 
    end 
end 

文件「 spec/factories/units.rb「:

FactoryGirl.define do 
    factory :taza , :class => CustomUnit do 
    singular 'taza' 
    plural 'tazas' 
    physical_type Unit::VOLUME 
    equivalence_factor 200 
    equivalence_unit 'ml' 
    end 

[other factories...]  
end 
+0

在'spec_helper'你是否需要工廠文件? – shosti

回答

4

我認爲這個問題是在這條線

FactoryGirl.find_definitions 

其實沒有必要爲這條線時,你的工廠都在正確的目錄中(我看到它是),以及你的Gemfile把gem factory_girl_rails

我想,20%的時間,第二次測試首先得到運行。目前沒有Factory的定義,測試失敗。另一個測試有這樣的定義並被通過。在其他時間,第一次測試首先如此定義存在。

我的建議:

  1. 請確保您有factory_girl_rails,在Gemfile中不factory_girl
  2. 刪除該定義。
  3. [可選但推薦]將所有定義放在一個文件中spec/factories並刪除所有其他工廠文件(如果沒有太多的工廠)。這將更容易管理。
+0

它似乎工作!謝謝!! 小記:在Gemfile中我加入了: gem'factory_girl_rails',github:'thinkbot/factory_girl_rails' – Attilio

相關問題