【问题标题】:Guard not loading Capybara DSLGuard 不加载 Capybara DSL
【发布时间】:2014-06-16 09:04:06
【问题描述】:

我正在运行 Rails 4,尝试使用 Rspec 和 Capybara 设置一些集成测试。我想设置警卫来运行“宙斯测试”。每当我做出改变时。问题是,每当调用 Capybara DSL 的任何内容,或者每当我尝试使用路由助手时,都会出现如下错误:

undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f55682c6d60>

如果我将 root_path 替换为 '/' 它会得到:

undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f556833bbb0>

如果我只运行 'rspec spec .'或“宙斯测试”。它工作正常。

我已尝试删除 'cmd: 'zeus test 。'我的 Guardfile 中的选项,但我遇到了同样的问题。很明显,问题出在 Guard 身上,与 zeus 无关。

在我的 Gemfile 中:

group :development, :test do
   gem 'capybara'
   gem 'rspec-rails'
   gem 'factory_girl_rails'
   gem 'guard-rspec', require: false
   gem 'better_errors'
   gem 'binding_of_caller'
end

规范助手:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

 config.include FactoryGirl::Syntax::Methods
 config.fixture_path = "#{::Rails.root}/spec/fixtures"

 config.use_transactional_fixtures = true

 config.infer_base_class_for_anonymous_controllers = false

 config.order = "random"
end

我正在尝试运行的规范:

require 'spec_helper'
describe "HomePage" do
  describe "Root page" do
  before { visit root_path }
    it "works!" do
      page.status_code.should be(200)
    end
    it 'contains bottom nav buttons' do
      page.should have_link 'How it works'
      page.should have_link 'Customer Service'
      page.should have_link 'Terms of Service'
      page.should have_link 'Contact Us'
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rspec capybara guard


    【解决方案1】:

    我通过在我的 spec_helper.rb 中注释掉这一行来解决这个问题:

    require 'rspec/autorun'
    

    同样,问题仅出在 Guard 上,我想自动运行会以某种方式跳过 spec_helper 中的配置块,所以现在一切正常。希望这可以帮助遇到同样问题的其他人。

    【讨论】:

      【解决方案2】:

      尝试将url_helpers添加到spec_helper.rb

      RSpec.configure do |config|
        ...
        config.include Rails.application.routes.url_helpers
      end
      

      并检查我的其他 answer 是否缺少 visit 方法

      【讨论】:

      • 谢谢,但我在问题中确实提到它在 Guard 之外也能正常工作。我尝试了这个以及 config.include Capybara::DSL,但都没有帮助 Guard 工作。
      【解决方案3】:

      尝试添加

      config.include Capybara::DSL
      

      致您的spec_helper.rb

      【讨论】:

      • 欢迎来到 Stack Overflow!这个答案出现在低质量的审查队列中,大概是因为你没有解释这是做什么的。如果你确实解释了这一点(在你的回答中),你更有可能获得更多的支持——提问者实际上学到了一些东西!
      • 当然。问题看起来像 Capybara 只是没有连接到 Rspec 测试。当我们将 config.include Capybara::DSL 添加到阻止 RSpec.configure do |config| ... end 时,它会将 Capybara DLS 加入您的测试。有时我们会忘记第一步。
      猜你喜欢
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多