Created
February 24, 2015 22:20
-
-
Save istana/adec4147c440664edb63 to your computer and use it in GitHub Desktop.
minitest with DatabaseCleaner transactions, elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'minitest' | |
gem 'minitest-rails' | |
group :development, :test do | |
# run when something change | |
gem 'guard' | |
gem 'guard-livereload' | |
# gem 'guard-rails_best_practices' | |
gem 'guard-minitest' | |
# factories | |
# gem 'fabrication' | |
gem 'fabrication', github: 'paulelliott/fabrication' | |
# mocks and stubs | |
gem 'mocha' | |
# test suite | |
# gem 'minitest' | |
# gem 'minitest-rails' | |
# output formatter | |
gem 'minitest-reporters' | |
#gem 'minitest-colorize' | |
# clean database when needed | |
gem 'database_cleaner' | |
# integration testing | |
gem 'capybara' | |
gem 'minitest-rails-capybara' | |
gem 'poltergeist' | |
gem 'connection_pool' | |
gem 'capistrano', '~> 3.3.0', require: false | |
gem 'capistrano-rails', '~> 1.1.2', require: false | |
gem 'capistrano-bundler', require: false | |
gem 'capistrano-rbenv', '~> 2.0', require: false | |
gem 'cape', require: false | |
# noecho password to the ssh key | |
gem 'highline', require: false | |
# gem 'capistrano3-unicorn', require: false | |
gem 'capistrano-unicorn-edge', require: false | |
gem 'jslint_on_rails', require: false | |
end | |
gem 'test_after_commit', :group => :test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = "test" | |
require File.expand_path('../../config/environment', __FILE__) | |
require 'rails/test_help' | |
require 'minitest/autorun' | |
require "minitest/reporters" | |
require "minitest/rails" | |
require "minitest/rails/capybara" | |
require "capybara/rails" | |
require 'capybara/poltergeist' | |
require 'mocha/mini_test' | |
require 'fabrication' | |
require 'database_cleaner' | |
require 'connection_pool' | |
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | |
## monkeypatch for AR use shared connection | |
# now poltergeist should work along with transactional fixtures | |
class ActiveRecord::Base | |
mattr_accessor :shared_connection | |
@@shared_connection = nil | |
def self.connection | |
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection } | |
end | |
end | |
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection | |
# transactions are _much_ faster, but have some limitations: | |
# - after_commit callback in models won't work | |
# - and poltergeist runs in another thread and data from transaction aren't available | |
# but works with patch above | |
DatabaseCleaner.strategy = :transaction | |
#DatabaseCleaner.strategy = :truncation | |
Capybara.javascript_driver = :poltergeist | |
Capybara.default_driver = :poltergeist | |
# clean all database beforehand | |
DatabaseCleaner.clean_with(:truncation) | |
#def MiniTest.filter_backtrace(bt) | |
# bt | |
#end | |
class ActionController::TestCase | |
include Devise::TestHelpers | |
end | |
class ActiveSupport::TestCase | |
ActiveRecord::Migration.check_pending! | |
self.use_transactional_fixtures = true | |
def setup | |
DatabaseCleaner.start | |
end | |
def teardown | |
DatabaseCleaner.clean | |
Category.__elasticsearch__.create_index!(force: true) | |
# Category.tire.create_elasticsearch_index | |
Advert.__elasticsearch__.create_index!(force: true) | |
# Advert.tire.create_elasticsearch_index | |
Support::Area.__elasticsearch__.create_index!(force: true) | |
# Support::Area.tire.create_elasticsearch_index | |
end | |
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. | |
# | |
# Note: You'll currently still have to declare fixtures explicitly in integration tests | |
# -- they do not yet inherit this setting | |
fixtures :all | |
# Add more helper methods to be used by all tests here... | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment