繁体   English   中英

Capybara:未定义的方法'访问'

[英]Capybara: undefined method 'visit'

当使用rspec和capybara运行我的规格时,它无法找到capybara的访问方法。 我需要做另一个初始化步骤吗?

$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)

的Gemfile:

group :test, :development do
  gem "rspec-rails"
  gem "capybara"
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'
require 'rspec/autorun'

homepage_spec.rb:

require 'spec_helper'

describe "The home page" do

  context "home page exists" do
    visit "/"
    page.should have_content("elephants")
  end
end

我自己也遇到过这个问题。

所以造成这种情况的原因是Capybara有一些无证的变化。 Capybara现在假设使用它的任何东西都需要在spec/features文件夹中,它会做出正确的假设。 spec/requests文件夹中剩下的任何内容都将无法使用。 虽然有解决方法。

对于上下文块,您可以添加参数:type => :feature ,这将解决该问题,或者您可以在spec to feature的开头更改describe方法的名称,这也应该更改它。

他们在Google群组中宣布了此更改: https//groups.google.com/forum/?fromgroups =#!topic /ruby-capybara / 5KfxezI-U0Q

值得注意的是,我们更改了:Capybara假设你的规格在RSpec中运行的类型:feature(以前是:request)。 最新版本的规格/功能。 或者你可以使用Capybara Feature DSL(功能而不是描述),它可以在没有任何额外调整的情况下工作。 如果您看到未定义方法访问等错误,那么您可能遇到此问题。 如果您将模块包含在:request specs中,您可能需要将其更改为:feature。

这在github问题中进一步讨论: https//github.com/jnicklas/capybara/issues/814

这里有几点需要注意:

  1. Capybara 2.0.x中的更改记录在此处https://github.com/rspec/rspec-rails/blob/master/Capybara.md spec目录结构有变化:spec / features,spec / controllers,spec / views,spec / helpers,spec / mailers。
  2. 在spec_helper中明确加载Capybara dsl


       require 'capybara/rails'
       require 'capybara/rspec'
       include Capybara::DSL

这对我有用。

require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'

RSpec.configure do |config|
  config.include Capybara::DSL, :type => :request
end

这使您可以在规范/请求中使用Capybara的帮助程序。

因为RSpec.configure不包括spec_helper.rb中的capybara DSL

这是一个丑陋的解决方案,但您可以将其添加到spec_helper.rb中。

module ::RSpec::Core
  class ExampleGroup
    include Capybara::DSL
    include Capybara::RSpecMatchers
  end
end 

这个git问题:

https://github.com/rspec/rspec-rails/issues/503

不幸的是,这种解决方法不适合我。 我还是得到的

NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>

回购是公开的: https//github.com/ikusei/Goldencobra_Newsletter您需要查看分支'28817499-subscribe'

编辑:如果我在我的描述块中包含Capybara :: DSL它可以工作。

但不推荐在全球范围内包括Capybara :: DSL!

因为我不知道一个好方法。

对于rspec 3和rails,请确保使用require "rails_helper" ,而不是require "spec_helper"

否则,请查看对rspec 3&rspec-railsCapybara 2.0.x的最新更改。

暂无
暂无

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

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