0
votes

Am using 'devise' gem for authentication and rspec for testing. My problem is after spec execution the test data is not getting cleared from DB, because of this subsequent execution of specs fail. Following is the spec:

describe User do

it "should return a valid user when valid email and password are used" do user = User.new(:email => '[email protected]', :password => 'test123', :password_confirmation => 'test123') user.save user.should be_valid end

end

Is there anything more that i am excepted to do here?

2
I've found DatabaseCleaner to be helpful in truncating the database during testing. github.com/DatabaseCleaner/database_cleanerInt'l Man Of Coding Mystery

2 Answers

0
votes

I'm not sure myself. I'm learning how to make gems and I ran across this issue. I added the method in spec_helper:


def purge_db
  [User, Subscription, Dorkus].each {|c| c.delete_all}
end

Spec::Runner.configure do |config|
  config.before(:each) { purge_db }
end         
0
votes

In some cases it is common practice to prepare the test db before running specs each time:

rails db:test:prepare