---
# System prepended metadata

title: 安裝 databasecleaner
tags: [安裝套件 相關]

---

---
title:  安裝 databasecleaner
tags: 安裝套件 相關
---

## <font color=red>**安裝 databasecleaner** </font>

安裝 databasecleaner 清除測試資料庫

https://medium.com/brief-stops/testing-with-rspec-factorygirl-faker-and-database-cleaner-651c71ca0688


在`rails helper`文件裡, 改下面內容 

`config.use_transactional_fixtures = true` to `config.use_transactional_fixtures = false`

建立 `spec/support/database_cleaner.rb` 
加上下列內容:

```
RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
```


`rails_helper` 裡加上這行(把註解拿掉)

`Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }`