Capybara:未定义的方法“访问”

发布于 2024-12-29 20:57:31 字数 920 浏览 4 评论 0原文

当使用 rspec & 运行我的规格时水豚,找不到水豚的访问方法。我还需要执行其他初始化步骤吗?

$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

When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?

$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

top of my 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断桥再见 2025-01-05 20:57:31

我自己刚刚遇到这个问题。

原因是 Capybara 发生了一些未记录的变化。 Capybara 现在假设任何使用它的东西都需要位于 spec/features 文件夹中,并且它将做出正确的假设。 spec/requests 文件夹中留下的任何内容都将不再起作用。尽管有解决方法。

对于上下文块,您可以添加参数 :type => :feature 这将解决该问题,或者您可以将规范开头的 describe 方法的名称更改为 feature ,这应该将其更改为出色地。

他们在 Google 群组中宣布了这一变化:https://groups.google。 com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q

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

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

Just ran into this issue myself.

So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.

For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.

They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q

Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.

This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814

温柔戏命师 2025-01-05 20:57:31

这里需要注意一些事项:

  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

A few things to note here :

  1. The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
  2. load Capybara dsl explicitly inside your spec_helper


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

花辞树 2025-01-05 20:57:31

这对我有用。

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

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

这使您能够在规范/请求中使用 Capybara 的助手。

This worked for me.

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

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

This enables you to use Capybara's helpers inside spec/requests.

眼眸印温柔 2025-01-05 20:57:31

因为 RSpec.configure 在spec_helper.rb中不包括水豚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

Because RSpec.configure not including capybara DSL in spec_helper.rb

It is an ugly solution, but you can add this to your spec_helper.rb.

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

The git issue for this:

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

初雪 2025-01-05 20:57:31

不幸的是,这个解决方法并不适合我。我仍然得到

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

The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
您需要查看分支“28817499-subscribe”

编辑:如果我将 include Capybara::DSL 放入我的描述块中,它就可以工作。

但不建议在全局范围内包含 Capybara::DSL!

因为我不知道有什么好的办法。

Unfortunately this workaround doesn't do it for me. I still get

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

The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'

edit: If i put include Capybara::DSL inside my describe block it works.

but including Capybara::DSL in the global scope is not recommended!

Because I do not know a good way.

惜醉颜 2025-01-05 20:57:31

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

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

For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".

Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文