ruby-on-rails – 水豚:未定义的方法“访问”

当使用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现在假设任何使用它需要在spec / features文件夹,它会做出正确的假设。任何遗留在spec / requests文件夹中的内容将不再有效。虽然有解决方法。

对于上下文块,您可以添加参数:type => :功能,这将解决这个问题,或者你可以更改名称的描述方法在规范的开头处的功能,这也应该改变它。

他们在他们的Google群组中宣布了这项变更: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.

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

http://stackoverflow.com/questions/9059854/capybara-undefined-method-visit

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:ruby-on-rails – 水豚:未定义的方法“访问”