ruby on rails - Resetting the database between rspec runs -
ruby on rails - Resetting the database between rspec runs -
i have test this, fails after first run
it 'should have login count of 1' ... (creates user 'test@example.com' email) expect(user.login_count).to eq 1 end
the test fails because each time run it, user still in database , login count keeps incrementing.
the login_count keeps incrementing because of method in programme either finds or creates user, each time though, increments login_count 1
what's best way have test think database clear each time runs?
in order have clean state in database among each test execution may want utilize database cleaner gem: https://github.com/databasecleaner/database_cleaner
rspec illustration setup (extracted github)
rspec.configure |config| config.before(:suite) databasecleaner.strategy = :transaction databasecleaner.clean_with(:truncation) end config.around(:each) |example| databasecleaner.cleaning example.run end end
ruby-on-rails rspec rspec-rails
Comments
Post a Comment