1

I have two rake tasks in one file. seed and populate. seed is creating a necessary data, and populate is populating sample data for my tests.

In my tests I am manipulating a database(adding the new entry, removing it and etc.). I am using Database cleaner gem to reseting the database However when I reset the database I need the populate data for the next test. I have the following setting but it is not working the database is still empty and the populate data is not there .

this is the spec/support/database_cleaner.rb

require 'rake'
load File.expand_path("../../../lib/tasks/my_tasks.rake", __FILE__)
RSpec.configure do |config|
  config.use_transactional_fixtures = false 
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    Rake::Task.define_task(:environment)
    Rake::Task['db:seed'].invoke
    Rake::Task['db:populate'].invoke
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

spec/rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'rake'

require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
 require 'spec_helper'
 require 'factory_girl_rails'
 require 'rspec/rails'
 require 'capybara/poltergeist'

 # Checks for pending migrations before tests are run.
 # If you are not using ActiveRecord, you can remove this line.
 ActiveRecord::Migration.maintain_test_schema!

 Capybara.configure do |config|
   config.default_wait_time = 20
 end

 RSpec.configure do |config|
 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
 #
 #  config.fixture_path = "#{::Rails.root}/spec/fixtures"

 config.include FactoryGirl::Syntax::Methods

 # If you're not using ActiveRecord, or you'd prefer not to run each of your 
 # examples within a transaction, remove the following line or assign false
 # instead of true.

 config.infer_spec_type_from_file_location!
end
click
  • 447
  • 1
  • 7
  • 25

1 Answers1

0

I found a way around it but this is not perfect and I am still looking for the solution of this so please post an answer if you find it. I am not going to select this as the correct answer.

You need to place a content spec/support/database_cleaner.rb file into the spec/spec_helper.rb file.

RSpec.configure do |config| 
.
.
.
  config.use_transactional_fixtures = false 
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    Rake::Task.define_task(:environment)
    Rake::Task['db:seed'].invoke
    Rake::Task['db:populate'].invoke
  end

  config.after(:each) do
    # DatabaseCleaner.clean
  end
  config.infer_spec_type_from_file_location!
end

Important: You need to comment or delete the: DatabaseCleaner.clean

click
  • 447
  • 1
  • 7
  • 25