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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -