ruby on rails - RSpec leaves record in test database -


whenever run user test, rspec leaves fabricated user in test database after test has completed, messing other tests. rake db:test:prepare, when run tests again, record recreated in database. have no idea why happening. happens user objects.

in spec_helper file have:

config.use_transactional_fixtures = true 

here example test creates record:

it "creates password reset token user"   alice = fabricate(:user)    post :create, email: alice.email   expect(assigns(alice.password_reset_token)).to_not eq(nil) end 

fabricator:

fabricator(:user)   email { faker::internet.email }   password 'password'   name { faker::name.name } end 

could have users model?

you should use gem called database_cleaner truncate database , reset automatically in gem file add gem database_cleaner after inside spec_helper.rb configure it

spec_helper.rb

config.use_transactional_fixtures = false  config.before(:suite)   databasecleaner.strategy = :truncation end  config.before(:each)   databasecleaner.start end  config.after(:each)   databasecleaner.clean end 

and create new file in spec/support directory

spec/support/shared_db_connection.rb

classactiverecord::base   mattr_accessor :shared_connection   @@shared_connection = nil    def self.connection     @@shared_connection || retrieve_connection   end end activerecord::base.shared_connection=activerecord::base.connection 

now whenever run tests database reset.this taken book 'everyday rails testing rspec' aaron sumner


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -