Search code examples
ruby-on-railsruby-on-rails-4minitest

How to run rake task multiple times in same file in Rails


I have written multiple test cases in a file, and i want to run rake task in every test individually. When i run the test case with this command:

ruby -I test test/integration/my_test_file.rb 

Then rake task runs only once. And many of my tests are failing. But if i run all failing test individually with this command

ruby -I test test/integration/my_test_file.rb -n test_name

Then all tests are passing.

The problem is rake task is running only once in first case. But for individual test Rake task runs every time for each test case.

here is how i setup rake task in my test file

require "rake"
MyAppName::Application.load_tasks

and in every test case i have this:

Rake::Task['tasks:my_task_name'].invoke

So how can i solve my problem ?


Solution

  • ok i got the answer.

    here is how you can call Rake::Task['tasks:task_name'] again within same scope, whether you want to run rake task in your controller file or in test file.

    Whenever Rake::Task['tasks:my_task_name'].invoke execute then it sets a flag @already_invoked, which means that you cannot invoke that task again within same file. So you can do this

    Rake::Task['tasks:my_task_name'].invoke
    
    Rake::Task['tasks:send_overdue_reminders'].reenable
    

    Here Rake::Task['tasks:send_overdue_reminders'].reenable flips the state of @already_invoked and you can call the rake task multiple times in same file