Object Caching - notahat/machinist GitHub Wiki

Note: This was removed in commit 2839888c92da324a18682970fbde233a1064819d, so it's not relevant to Machinist ~> 2.0.

When you create an object with make!, Machinist 2 keeps the object around in the database. If, in a later test, you request an identical object, Machinist will give you the cached copy.

This is a big performance win, particularly when creating objects in the setup for a group of tests.

Problems

This is still an experimental feature, so there are some quirks.

  • Only works with ActiveRecord objects
  • You must have transactional fixtures enabled
  • It doesn't play nicely with cucumber, unless you manually clean your database before each run like so:
Before do
  DatabaseCleaner.clean
end

Disabling Caching

In your config/environments/test.rb or somewhere similar, do this:

Machinist.configure do |config|
  config.cache_objects = false
end

Add the following to you cucumber env.rb to clear things after each run:

at_exit do
  DatabaseCleaner.clean
end