【问题标题】:rspec capybara error - undefined method `visit' for #<RSpecrspec capybara 错误 - #<RSpec 的未定义方法“访问”
【发布时间】:2014-06-29 15:13:29
【问题描述】:

我有:

  • 已将gem 'capybara' 添加到我的Gemfile 并运行bundle

  • 已将我的规范作为 visit_home_page_root_spec.rb

  • 放入规范/请求中
  • 已添加到 `spec/spec_helper.rb:

-

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

但我仍然收到标题中显示的消息。

我的规格是:

describe "Home Page", :type => :feature do

  it "Visit the home page" do
    visit '/'
    expect(page).to have_content 'Linker'
  end

end

Gemfile.lock 有:

rspec-core (2.14.8)
rspec-expectations (2.14.5)
  diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
rspec-rails (2.14.1)
  actionpack (>= 3.0)
  activemodel (>= 3.0)
  activesupport (>= 3.0)
  railties (>= 3.0)
  rspec-core (~> 2.14.0)
  rspec-expectations (~> 2.14.0)
  rspec-mocks (~> 2.14.0)

【问题讨论】:

  • 是的:gem "rspec-rails", "~> 2.0" 这是 rspec 3.x 我相信
  • 不应该 - 实际解析的版本是什么(如果您使用的是捆绑程序,请检查 Gemfile.lock)
  • 从我的 Gemfile.lock 添加 rspec 信息
  • 我的 Gemfile 有 gem "rspec-rails", "~&gt; 2.0" 也许我应该删除 2.0 版本并尝试运行它们。
  • 我试过了,做了捆绑安装,然后运行测试,我得到了同样的错误。

标签: ruby-on-rails ruby-on-rails-3 rspec capybara rspec2


【解决方案1】:

听起来您的示例中没有包含 capybara DSL。这已经越来越少地自动完成(在 rspec 3 中根本没有),但从记忆中它应该是你的版本。

不过,你应该可以通过这样做来解决这个问题

describe "foo" do
  include Capybara::DSL
end

对于任何需要它的规格。或者,在你的 RSpec.configure 块中,做

config.include(Capybara::DSL, :type => :feature)

会自动为所有规格的类型功能执行此操作)

【讨论】:

  • 请注意,type 选项不再(自 RSpec 3 起)“神奇地”将配置选项应用于 spec/features 中的测试 - 有关详细信息,请参阅 myronmars.to/n/dev-blog/2014/05/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多