ruby-on-railselasticsearchminitestsearchkick

Run code after fixtures loading


I'm using Rails 5.1 with Minitest and Searchkick gem and in my system tests I need to have the data indexed in ElasticSearch to make sure the tests pass.

If I add a breakpoint and inspect

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all
  require 'pry'; binding.pry

  # Add more helper methods to be used by all tests here...
end

all my models have zero records, assuming I've recreated the database with: rails db:drop db:create db:migrate

So how can I have the code Model.reindex running after the loading of fixtures?

Note: I could use the setup but that way I will do a reindex in all needed models before each test, increasing the time.


Solution

  • You can use a class variable in your setup method like this:

    class SomeTest
      setup do
        @@once ||= Model.reindex
      end
    end