2014-07-19 34 views
1

create_list(:post, 3)導致唯一性驗證失敗。FactoryGirl belongs_to association的唯一性驗證失敗

我廠擁有郵政和作者之間的關聯belongs_to的:

factory :post do 
    author 
end 

factory :author do 
    sequence(:internal_ref) { |n| n } 
end 

Author模式對internal_ref唯一性驗證。

UPDATE

後下面的評論暗示清理數據庫,我試圖從一個全新的測試數據庫是這樣開始:

rake db:drop 
rake db:create 
rake db:schema:load 
rake # to create test db and run the tests 

,並使用database_cleaner_gem像這樣spec_helper:

config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
end 
+0

我有同樣的問題。我會有興趣聽到其他人想出的內容,但我調整了Rspec.config數據庫清除設置後,我失蹤了。https://github.com/DatabaseCleaner/database_cleaner#how-to-use – steel

+0

試試這篇文章。提出了一個解決方案,並且'爲什麼'在評論中:http://stackoverflow.com/questions/6091039/how-come-factory-girl-isnt-sequencing-unique-attributes – steel

+0

@steel我嘗試使用品牌新的數據庫和使用數據庫清理寶石,但我沒有得到相同的結果 – mehulkar

回答

1

關閉這個問題。看起來這個套件在seeds.rb中創建了一個作者記錄列表,並且在運行測試套件之前加載了seeds.rb。