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.

Examples:

class MyTest < Minitest::Test
  prepend Fixtury::MinitestHooks

  fixtury "user"
  fixtury "post"

  def test_something
    user # => returns the `users` fixture
    user.do_some_mutation
    assert_equal 1, user.mutations.count
  end
end

# In the above example, the `users` and `posts` fixtures will be loaded
# before the test case is setup, and any changes will be rolled back
# after the test case is torn down.

# The `fixtury` method also accepts a `:as` option, which can be used to
# define a named accessor method for a fixture. This is useful when
# defining a single fixture, and you want to access it using a different
# name. If no `:as` option is provided, the fixture will be accessed
# using the last segment of the fixture's pathname.

class MyTest < Minitest::Test
  prepend Fixtury::MinitestHooks

  fixtury "/my/user_record", as: :user

end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Raises:

  • (ArgumentError)


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_setupObject

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.

Parameters:

  • search (String)

    The search term to use to find the fixture.

Returns:

  • (Object)

    The fixture.

Raises:



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

#fixtury_setupObject

Load all dependenct fixtures and begin a transaction for each database connection.



135
136
137
138
# File 'lib/fixtury/minitest_hooks.rb', line 135

def fixtury_setup
  Fixtury.store.clear_stale_references!
  fixtury_load_all_fixtures!
end