[英]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: 回到Rails <= 4天,由于Rakefile中的默认设置,我们只需在命令行中输入$ rake
即可运行测试套件。
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的5它不是那么明显怎么办好,现在默认rake任务rake
已被取代的rails
。 rails
alone gives a list of possible commands rails responds to but doesn't run the specs. 仅rails
给出了rails响应但不运行规范的可能命令列表。 rails test
seems logical but it tries to run minitest which we don't use. rails test
似乎合乎逻辑,但是它尝试运行我们不使用的minitest。 rails spec
will run Rspec but not teaspoon or rubocop . rails spec
将运行Rspec的但不是茶匙或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: 只需创建一个运行其他任务的新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. 运行rails my_test
,您将在控制台中看到FOO
和BAR
。
If you don't know where to place the file to write the code above, check your /Rakefile
: 如果您不知道将文件放置在何处以编写上面的代码,请检查/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. 它说将它们写在lib/tasks
,并以.rake
,您不需要它们。 In your specific question, change my code from :foo
and :bar
to your specific tasks :rubocop :spec :teaspoon
. 在您的特定问题中,将我的代码从:foo
和:bar
更改为您的特定任务:rubocop :spec :teaspoon
。
However, it looks like you are doing some BDD or TDD cycle. 但是,看起来您正在执行一些BDD或TDD周期。 Check out rails Guard , it might help you better. 查看rails Guard ,它可能会对您有所帮助。 I use it in my project and it works perfectly. 我在项目中使用了它,并且效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.