Question

I'm very new to TDD, and have opted to go with the above mentioned Gems. I think I have set it up correctly as I can run my tests. I can't, however, figure out how to populate my test database from db/seeds.rb. When I invoke

rake db:seed RAILS_ENV=test

in the terminal, I can see the rows created in the database through PGAdmin. However, when I run my tests with the following

rake minitest:all

the database ends up being blank afterwards, and in the test when I save a screenshot, the items from the database does not appear in the frontend as it does when I'm in dev.

My test_helper.rb contains the following.

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'
Capybara.javascript_driver = :webkit

class ActiveSupport::TestCase
  fixtures :all
    DatabaseCleaner.strategy = :transaction

    class MiniTest::Spec
      before :each do
            Rake::Task["db:seed"].invoke
            DatabaseCleaner.start
      end

      after :each do
            DatabaseCleaner.clean
      end
    end
end

And for some extra background, my db/seeds.rb file (which works when seeded from manually using rake)

ProgramIndustry.delete_all
ProgramIndustry.create([
        { name: 'Accounting and finance'},
        { name: 'Banking'},
        { name: 'Construction'},
        { name: 'Education'}
])

Why would the database not be populated with seeds.rb when the tests start?

Was it helpful?

Solution

Your database is blank because you are using DatabaseCleaner, which removes the data from the database. I assume this is what you want your test_helper.rb file to look like:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

Capybara.javascript_driver = :webkit

class ActiveSupport::TestCase
  fixtures :all

  DatabaseCleaner.strategy = :transaction

  before do
    DatabaseCleaner.start
    Rake::Task["db:seed"].invoke # seed after starting
  end

  after do
    DatabaseCleaner.clean
  end
end

I don't know about invoking the db:seed task from the before hook, that seems kinda suspect. But I don't use DatabaseCleaner, as I prefer to use fixtures and the transactions supported by ActiveSupport::TestCase.

I don't know why you are using DatabaseCleaner, but seeing as you are using RSpec syntax in Minitest, I'm assuming you are just trying things until they work. Might I suggest the dropping DatabaseCleaner and put all your test data in fixtures and using the following to manage the database transactions across threads:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

class ActiveSupport::TestCase
  fixtures :all
end

# Capybara driver
Capybara.javascript_driver = :webkit

# Make all database transactions use the same thread
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def current_connection_id
    Thread.main.object_id
  end
end

And if you have issues with that, consider this variation:

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require 'minitest/rails/capybara'
require 'minitest/focus'
require 'minitest/colorize'

class ActiveSupport::TestCase
  fixtures :all
end

# Capybara driver
Capybara.javascript_driver = :webkit

# Make all database transactions use the same thread
class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top