Capybara:未定义的方法“访问”
当使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我自己刚刚遇到这个问题。
原因是 Capybara 发生了一些未记录的变化。 Capybara 现在假设任何使用它的东西都需要位于
spec/features
文件夹中,并且它将做出正确的假设。spec/requests
文件夹中留下的任何内容都将不再起作用。尽管有解决方法。对于上下文块,您可以添加参数
:type => :feature
这将解决该问题,或者您可以将规范开头的describe
方法的名称更改为feature
,这应该将其更改为出色地。他们在 Google 群组中宣布了这一变化:https://groups.google。 com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q
这在 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 thespec/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 adescribe
method at the beginning of a spec tofeature
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
This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814
这里需要注意一些事项:
A few things to note here :
这对我有用。
这使您能够在规范/请求中使用 Capybara 的助手。
This worked for me.
This enables you to use Capybara's helpers inside spec/requests.
因为 RSpec.configure 在spec_helper.rb中不包括水豚DSL,
这是一个丑陋的解决方案,但您可以将其添加到您的spec_helper.rb中。
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.
The git issue for this:
https://github.com/rspec/rspec-rails/issues/503
不幸的是,这个解决方法并不适合我。我仍然得到
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
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.
对于 rspec 3 和 Rails,请确保您使用的是
require "rails_helper"
,而不是require "spec_helper"
。否则,请查看 rspec 3 & 的最新更改rspec-rails 和 Capybara 2.0.x。
For rspec 3 and rails, make sure you are using
require "rails_helper"
, instead ofrequire "spec_helper"
.Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.