factory_girl + rspec doesn't seem to roll back changes after each example

前端 未结 7 2066
暗喜
暗喜 2021-02-07 05:39

Similar to the problem described here: http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec

in Short (shorten\'d code):

spec_helper:

c         


        
7条回答
  •  深忆病人
    2021-02-07 06:26

    Question: Shouldn't rspec clear database before each spec example and hence not throwing duplicate entry errors?

    RSpec with DatabaseCleaner or RSpec Rails with use_transactional_fixtures will clear the DB as long as your created the data in the example itself. before :all do ... end is considered outside of the example, because the data remains untouched across multiple examples. Whatever you create in before :all you have to delete in after :all.

    In order to delete whatever you create automatically use before :each do ... end. Be aware the same data will be created and removed 10 times if you have 10 examples. The difference between before :all and before :each is better explained here: rails rspec before all vs before each

提交回复
热议问题