简体   繁体   English

为什么默认rake任务在Rails应用程序中运行规范?

[英]Why does default rake task run specs in Rails app?

I have one app that runs the specs with just rake , but don't know where or how this task is defined. 我有一个应用程序只用rake运行规范,但不知道这个任务的定义在哪里或如何。 There are no tasks in lib/tasks. lib / tasks中没有任务。

Part of Gemfile: Gemfile的一部分:

group :test do
  gem 'capybara'
  gem 'guard-rspec'
  gem 'rspec-rails'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'oauth2'
  gem 'rack_session_access'
  gem 'factory_girl'
  gem 'webmock'
  gem 'selenium-webdriver'
end

RSpec gems: RSpec宝石:

guard-rspec (4.5.0)
rspec (3.1.0)
rspec-core (3.1.7)
rspec-expectations (3.1.2)
rspec-mocks (3.1.3)
rspec-rails (3.1.0)
rspec-support (3.1.2)

I'm using Rake 10.4.2 and Rails 4.1.6. 我正在使用Rake 10.4.2和Rails 4.1.6。

Also, when I add: 另外,当我添加:

task :default do
  puts 'No default task.'
end

to the Rakefile, first it runs the specs, and then prints "No default task." 到Rakefile,首先运行规范,然后输出“No default task”。

EDIT: Add 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 File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

If you have rspec-rails in your Gemfile, then when the gem is loaded by Rails is will execute this line: 如果您的Gemfile中有rspec-rails ,那么当Rails加载gem时,将执行以下行:

https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec-rails.rb#L19 https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec-rails.rb#L19

load "rspec/rails/tasks/rspec.rake"

which in turn defines the default rake task: 而这又定义了默认的rake任务:

https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec/rails/tasks/rspec.rake#L6 https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec/rails/tasks/rspec.rake#L6

task :default => :spec

That's why the default rake task runs the specs in your Rails app. 这就是默认rake任务运行Rails应用程序中的规范的原因。

Furthermore, when you added this code: 此外,当您添加此代码时:

task :default do
  puts 'No default task.'
end

That does not in fact redefine the default task: it augments it. 事实上,这并没有重新定义默认任务:它会增加它。 The way that Rake task definition works, each declaration of the same task name adds to that task . Rake任务定义的工作方式,相同任务名称的每个声明都会添加到该任务中 It does not redefine it. 它没有重新定义它。 Hence the result that you see "No default task" and the specs are run. 因此,您会看到“无默认任务” 运行规范的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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