簡體   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