【问题标题】:Guard runs my specs twiceGuard 运行我的规格两次
【发布时间】:2013-11-25 20:19:14
【问题描述】:

我很确定 Guard 是罪魁祸首,因为运行 rspec 会运行一次所有规范。运行 guardenter 会导致所有规范运行两次。不知道为什么。谷歌搜索到破坏,常见的问题,例如在 spec_helper.rb 中要求 'rspec/autorun' 根本不是它的原因。

当规格开始变慢时,它会变得很烦人!

.rspec

--color
--order default

保护文件

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard :rspec do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$})     { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    Capybara.run_server = false
    Capybara.javascript_driver = :webkit
    Capybara.default_selector = :css
    Capybara.server_port = 7171
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # config.include RSpec::Rails::RequestExampleGroup, type: :feature

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

更新

这可能会提供线索

$ guard
06:40:31 - INFO - Guard is using GNTP to send notifications.
06:40:31 - INFO - Guard is using TerminalTitle to send notifications.
06:40:31 - INFO - Guard::RSpec is running
06:40:31 - INFO - Guard::RSpec is running
06:40:31 - INFO - Guard is now watching at '/Users/starkers/Desktop/boxshare'
[1] guard(main)> 
06:40:35 - INFO - Run all
06:40:35 - INFO - Running all specs

请注意 06:40:31 的两个信息日志 Rspec 正在运行...

【问题讨论】:

  • 您使用的是什么版本的 Guard 和 RSpec?
  • 我在单独的终端窗口中运行 Guard/Spork 服务器。我注意到我的“守卫”终端窗口首先运行新测试,然后我的“主”窗口显示相同的输出。但是如果我重新运行相同的测试,'guard' 窗口不会重新显示。你为你的守卫服务器使用一个单独的窗口吗?
  • @Chiperific 我确实这样做了。
  • @DanielBerkompas 最新版本。是的,这在 6 个月后仍然是一个问题!
  • 我还在某些应用程序中间歇性地看到了这种行为,而在其他应用程序中则没有。奇数。

标签: ruby-on-rails rspec capybara


【解决方案1】:

FWIW,我也遇到了这个问题。 Guard 运行了我所有的 rspec 测试两次。事实证明,我已经运行了两次 guard init rspec 并且我的 Guardfileguard ... do ... end 节重复.... doh。

【讨论】:

  • 谢谢!我有同样的问题。我刚刚删除了 Guardfile 并运行了 Guard init rspec 以消除所有重复。
【解决方案2】:

我已经多次看到这个问题。该问题通常(但不总是)由重复的 RSpec 设置引起。尝试从 spec_helper.rb 中删除 config.order = "random" 行,因为您的 .rspec 文件中已经有了该设置。

【讨论】:

  • 本来可以的,但遗憾的是对我个人没有用。我已经用可能的线索更新了这个问题:)
【解决方案3】:

我的问题是选项all_after_pass

刚刚将我在Guardfile 上的守卫命令更改为guard :rspec, cmd: 'rspec', all_after_pass: false do,它就停止了。

https://groups.google.com/forum/#!topic/guard-dev/t1xxZpI5oWA https://github.com/guard/guard-rspec#list-of-available-options

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多