2012-11-21 65 views
1

考慮例如:FactoryGirl不會初始化工廠

*# spec/factory/sections.rb* 

FactoryGirl.define do 

    factory :any_section do 
     (...) 
    end 


    factory :fake_section do 
     (...) 
    end 


    factory :section do 
     (...) 
    end 
end 

我發現我的RSpec的測試只能看到:section工廠。所有其他人最終拋出一個錯誤,如:uninitialized constant FakeSection

這是爲什麼?

回答

0

的問題不在於FactoryGirl沒有看到any_sectionfake_section工廠,那就是你沒有指定自己的班級,因此它被猜測的名字(即any_section變得AnySectionfake_section成爲FakeSection)。

要解決該問題只指定類是Section使用class選項:

factory :any_section, :class => Section do 
    (...) 
end 


factory :fake_section, :class => Section do 
    (...) 
end 

又見this article

+0

就是這樣,謝謝! –