Why is my rake task running twice in my test?

后端 未结 4 1661
悲&欢浪女
悲&欢浪女 2021-01-12 17:23

I have a rake task test that I setup following the only examples I could find online.

It looks like this:

require \'test_helper\'
require \'minitest/         


        
4条回答
  •  天命终不由人
    2021-01-12 17:56

    A solution that works for testing the tasks of a Gem that has been made a Railtie so it can add tasks to the Rails app:

    Don't define the Railtie in test mode when you're also defining a Rails::Application class in spec_helper.rb (which allows your tests to call Rails.application.load_tasks). Otherwise the Rake file will be loaded once as a Railtie and once as an Engine:

    class Railtie < Rails::Railtie
      rake_tasks do
        load 'tasks/mygem.rake'
      end
    end unless Rails.env.test? # Without this condition tasks under test are run twice
    

    Another solution would be to put a condition in the Rake file to skip the task definitions if the file has already been loaded.

提交回复
热议问题