Created
December 13, 2018 12:42
-
-
Save v-kolesnikov/62ea1590bb6d134c7e85e1658daa2266 to your computer and use it in GitHub Desktop.
Using a container to manage tests components
This file contains 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
# frozen_string_literal: true | |
require_relative '../system/my_app/container' | |
module MyApp | |
module Tests | |
class Container < Dry::System::Container | |
use :env, inferrer: -> { 'test' } | |
configure do | |
config.name = :my_app_tests | |
end | |
import core: MyApp::Container | |
load_paths! 'spec' | |
end | |
end | |
end | |
MyApp::Tests::Container.boot :rspec do |system| | |
init do | |
require 'rspec/collection_matchers' | |
RSpec.configure do |config| | |
config.expect_with :rspec do |expectations| | |
expectations.include_chain_clauses_in_custom_matcher_descriptions = true | |
end | |
config.mock_with :rspec do |mocks| | |
mocks.verify_partial_doubles = true | |
end | |
config.default_formatter = 'doc' if config.files_to_run.one? | |
config.disable_monkey_patching! | |
config.shared_context_metadata_behavior = :apply_to_host_groups | |
config.warnings = false | |
config.define_derived_metadata do |meta| | |
meta[:aggregate_failures] = true | |
end | |
config.order = :random | |
Kernel.srand config.seed | |
end | |
end | |
end | |
MyApp::Tests::Container.boot :rack_test do | |
init do | |
use :rspec | |
require 'rack/test' | |
require 'my_app/web' | |
RSpec.configure do |config| | |
config.include Rack::Test::Methods, type: :request | |
config.include((Module.new do | |
def app | |
MyApp::Web.freeze.app | |
end | |
end), type: :request) | |
end | |
end | |
end | |
MyApp::Tests::Container.boot :database_cleaner do |system| | |
init do | |
use :rspec | |
require 'database_cleaner' | |
DatabaseCleaner.allow_remote_database_url = true | |
database_cleaner = DatabaseCleaner[ | |
:sequel, connection: system[:'core.persistence.database'] | |
] | |
RSpec.configure do |config| | |
config.before :suite do | |
database_cleaner.strategy = :deletion | |
database_cleaner.start | |
database_cleaner.strategy.clean | |
end | |
config.around do |example| | |
database_cleaner.cleaning do | |
example.run | |
end | |
end | |
end | |
end | |
end | |
MyApp::Tests::Container.boot :factory do |system| | |
init do | |
use :rspec | |
require 'rom-factory' | |
factory = ROM::Factory.configure do |config| | |
config.rom = system[:'core.persistence.rom'] | |
end | |
factory.define :provider do |f| | |
f.title { fake(:company, :name) } | |
f.created_at { Time.now } | |
f.updated_at { Time.now } | |
end | |
register :factory, factory | |
RSpec.configure do |config| | |
config.include(Module.new do | |
def factory | |
MyApp::Tests::Container[:factory] | |
end | |
end) | |
end | |
end | |
end | |
MyApp::Tests::Container.finalize! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usually
spec_helper.rb
is a mess of global constants, rspec configs and $LOAD_PATH preparations. Time to change it!