config.cache_classes = false messing up rspec tests?

前端 未结 2 1915
旧巷少年郎
旧巷少年郎 2020-12-21 01:28

I\'m following the Ruby on Rails Tutorial by Michael Hartl (railstutorial.org).

At some point I got tired of tests failing just bescause the tests used old cached v

2条回答
  •  醉梦人生
    2020-12-21 02:23

    I had exactly the same problem and like you setting config.cache_classes to true solved the problem. But caching classes in the test environment really isn't what you want I don't think. I certainly don't understand why caching classes makes the tests pass.

    So the way I found to solve this was to install a database cleane as the reason the tests are failing is due to duplicate entries in the test database. https://github.com/bmabey/database_cleaner

    Then in your gemfile, in your test group add this.

    gem 'database_cleaner', '0.6.6'
    

    then run "bundle install" to install this gem

    Then... in your spec_helper.rb file, add this..

    RSpec.configure do |config|
    .
    .
    .
    .
       config.before(:each) do
          DatabaseCleaner.strategy = :truncation
          DatabaseCleaner.clean
       end
    

    This will clear your test database before each run of your rspec tests.

    Hope this helps. Cheers, mark.

提交回复
热议问题