Module: Fixtury::MinitestHooks
- Defined in:
- lib/fixtury/minitest_hooks.rb
Overview
MinitestHooks is a module designed to hook into a Minitest test case, and provide a way to load fixtures into the test case. It is designed to be prepended into the test case class, and will automatically load fixtures before the test case is setup.
The module also provides a way to define fixture dependencies, and will automatically load those dependencies before the test case is setup.
Use ‘as: false` if you do not want an accessor created.
A Set object named fixtury_dependencies is made available on the test class. This allows you to load all Minitest runnables and analyze what fixtures are needed. This is very helpful in CI pipelines when you want to prepare all fixtures ahead of time to share between multiple processes.
It is the responsibility of the suite to manage the snapshot or rollback of the database. Generally something like ActiveRecord’s use_transactional_fixtures will work just fine.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#before_setup ⇒ Object
Minitest before_setup hook.
-
#fixtury(search) ⇒ Object
Access a fixture via a search term.
-
#fixtury_load_all_fixtures! ⇒ void
Load all fixture dependencies that have not previously been loaded into the store.
-
#fixtury_setup ⇒ Object
Load all dependenct fixtures and begin a transaction for each database connection.
Class Method Details
.included(klass) ⇒ Object
63 64 65 |
# File 'lib/fixtury/minitest_hooks.rb', line 63 def self.included(klass) raise ArgumentError, "#{name} should be prepended, not included" end |
.prepended(klass) ⇒ Object
57 58 59 60 61 |
# File 'lib/fixtury/minitest_hooks.rb', line 57 def self.prepended(klass) klass.class_attribute :fixtury_dependencies klass.fixtury_dependencies = Set.new klass.extend ClassMethods end |
Instance Method Details
#before_setup ⇒ Object
Minitest before_setup hook. This will load the fixtures before the test.
112 113 114 115 |
# File 'lib/fixtury/minitest_hooks.rb', line 112 def before_setup(...) fixtury_setup if fixtury_dependencies.any? super end |
#fixtury(search) ⇒ Object
Access a fixture via a search term. This will access the fixture from the Fixtury store. If the fixture was not declared as a dependency, an error will be raised.
124 125 126 127 128 129 130 131 132 |
# File 'lib/fixtury/minitest_hooks.rb', line 124 def fixtury(search) dfn = Fixtury.schema.get!(search) unless fixtury_dependencies.include?(dfn.pathname) raise Errors::UnknownTestDependencyError, "Unrecognized fixtury dependency `#{dfn.pathname}` for #{self.class}" end Fixtury.store.get(dfn.pathname) end |
#fixtury_load_all_fixtures! ⇒ void
This method returns an undefined value.
Load all fixture dependencies that have not previously been loaded into the store.
143 144 145 146 147 148 149 150 |
# File 'lib/fixtury/minitest_hooks.rb', line 143 def fixtury_load_all_fixtures! fixtury_dependencies.each do |name| next if Fixtury.store.loaded?(name) ::Fixtury.log("preloading #{name.inspect}", name: "test", level: ::Fixtury::LOG_LEVEL_INFO) fixtury(name) end end |