简体   繁体   中英

How to run default rake tasks in Rails 5?

Here's a layup for someone...

Back in Rails <= 4 days we'd run our test suite by simply typing $ rake at the command line, thanks to defaults in Rakefile:

task default: [:rubocop, :spec, :teaspoon]

but in Rails 5 it's not so apparent how to run default rake tasks now that rake has been replaced by rails . rails alone gives a list of possible commands rails responds to but doesn't run the specs. rails test seems logical but it tries to run minitest which we don't use. rails spec will run Rspec but not teaspoon or rubocop .

Where did this go? And why is something so apparently simple so hard for me to look up myself?

尽管我在任何地方都找不到文档,但是rails default在Rails 5.2.1上为我执行了这些任务。

Just create a new rake task that runs the other ones:

lib/tasks/my_extensions.rake
 task :my_test do Rake::Task[:foo].invoke Rake::Task[:bar].invoke end # or the short version: # task my_test: [:foo, :bar] task :foo do puts "FOO" end task :bar do puts "BAR" end 

Run rails my_test and you will see FOO and BAR printed in your console.

If you don't know where to place the file to write the code above, check your /Rakefile :

 # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. require_relative 'config/application' Rails.application.load_tasks 

It says to write them inside lib/tasks and end them with .rake , you don't need to require them. In your specific question, change my code from :foo and :bar to your specific tasks :rubocop :spec :teaspoon .

However, it looks like you are doing some BDD or TDD cycle. Check out rails Guard , it might help you better. I use it in my project and it works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM