2010-08-13 68 views
3

我被困在實施測試時略有混淆。隨着User.create我可以創建並保存在多個測試:Factory Girl創建的對象在測試之間沒有清除?

should "test something" do 
    u1 = User.create(:first_name => "Fred", :last_name => "Flintstone") 
    assert true 
end 

should "test something else" do 
    u1 = User.create(:first_name => "Fred", :last_name => "Flintstone") 
    assert true 
end 

但使用Factory.create,它拋出一個ActiveRecord的重複輸入錯誤:

should "test something" do 
    Factory.create(:amanda_levy) 
    assert true 
end 

should "test something else" do 
    Factory.create(:amanda_levy) 
    assert true 
end 

錯誤:「的ActiveRecord :: StatementInvalid:Mysql的::錯誤:重複項「

什麼給?

+0

什麼是:amanda_levy,這可能是問題的來源 – s84 2010-08-13 06:22:41

+0

這只是Factory.create的工廠女孩​​語法(:factory_name) – ambertch 2010-08-16 02:38:47

回答

1

你有沒有下面一行在你spec_helper:

config.use_transactional_fixtures = true 

這告訴RSpec的/測試::單位在每個測試用例的開始 啓動一個事務,併發出回滾時,它已經結束了。

+1

值得檢查您的測試數據庫是否支持事務。例如,使用默認MyISAM存儲引擎的mysql不會。 – Shadwell 2010-08-13 10:08:04

相關問題