Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
Paulo Fidalgo Avatar asked May 09 '26 15:05

Paulo Fidalgo


1 Answers

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

class SomeTest
  setup do
    @@once ||= Model.reindex
  end
end
like image 94
Andrew Kane Avatar answered May 12 '26 04:05

Andrew Kane