在Rails中使用RSpec和Capybara时未定义的方法'visit' [英] undefined method `visit' when using RSpec and Capybara in rails

查看:50
本文介绍了在Rails中使用RSpec和Capybara时未定义的方法'visit'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让水豚使用rspec。它给了我这个错误:

I can't get capybara working with rspec. It gives me this error:

undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>

我知道有很多关于此的帖子,但是没有一种解决方案对我有用。其中大多数涉及的规范不在/ spec / features中-属于我的。

I know there are lots of posts about this but non of the solutions are working for me. Most of them involve the specs not being in /spec/features - which mine is in.

首先出现错误:

First the error:

$bundle exec rspec spec
F

Failures:

  1) security signs users in
     Failure/Error: visit "/sessions/new"
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x16529f8 @example=nil>
     # ./spec/features/security_spec.rb:4:in `(root)'

 Finished in 0.006 seconds
 1 example, 1 failure

Failed examples:

rspec ./spec/features/security_spec.rb:3 # security signs users in

$ b中签名用户
$ b

我认为重要的是要注意,起初我使用的是URL Helper'new_sessions_path',但它一直给我一个错误未定义的局部变量或方法'new_sessions_path'。我知道它是有效的,因为:

I think its important to note that at first I was using the URL Helper 'new_sessions_path' and it kept giving me an error undefined local variable or method 'new_sessions_path'. I know it is valid because:

$ rake routes
logout_sessions GET    /sessions/logout(.:format) sessions#logout
       sessions POST   /sessions(.:format)        sessions#create
   new_sessions GET    /sessions/new(.:format)    sessions#new
      contracts POST   /contracts(.:format)       contracts#create
  new_contracts GET    /contracts/new(.:format)   contracts#new
 edit_contracts GET    /contracts/edit(.:format)  contracts#edit
                GET    /contracts(.:format)       contracts#show
                PUT    /contracts(.:format)       contracts#update
                DELETE /contracts(.:format)       contracts#destroy
           root        /                          contracts#index

我的Gemfile:

My Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.11'
gem 'execjs'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'activerecord-oracle_enhanced-adapter', '~> 1.4.1'
gem 'jruby-openssl'
gem 'therubyrhino'
gem 'kaminari'
gem 'nokogiri'

group :development do
  gem 'warbler'
end

group :test do
  gem 'rspec-rails'
  gem 'capybara'
  gem 'activerecord-jdbcsqlite3-adapter'
end

spec_helper.rb在my_app / spec中:

spec_helper.rb inside of my_app/spec:

# 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 'rspec/autorun'

# Capybara integration
require 'capybara/rspec'
require 'capybara/rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
  # Include path helpers
  config.include Rails.application.routes.url_helpers
end

my_app / spec / features / security_spec.rb:

my_app/spec/features/security_spec.rb:

describe "security", :type => :feature do
  it "signs users in" do
    visit "/sessions/new"
    fill_in "username", :with => "user"
    fill_in "password", :with => "pass"
    click_button "Sign In"

    page.should have_content('Login Successful')
  end
end

我尝试定义上面的测试,无论是否使用:type => :功能。两种方式都没有区别。有什么想法我下一步应该尝试什么?

I've tried defining the test above both with and without :type => :feature. It makes no difference either way. Any ideas what I should try next?

推荐答案

添加需要'rails_helper'在我的功能的顶部最终解决了我的问题:

Adding require 'rails_helper' at the top of my feature ended up fixing my problem:

require 'rails_helper'

describe "security", :type => :feature do

  it "signs users in" do
    visit new_sessions_path
    fill_in "username", :with => "user"
    fill_in "password", :with => "pass"
    click_button "Sign In"

    page.should have_content('Login Successful')
  end
end

这对我来说似乎很奇怪,因为我为rspec和capybara看到的每个示例都没有这个要求,但是很好。问题已解决。

This seems odd to me since every example I've seen for rspec and capybara didn't have that require, but oh well. Problem solved.

require'spec_helper '由RSpec的较早版本使用。更好的答案是需要 rails_helper

require 'spec_helper' is used by older versions of RSpec. The better answer would be require 'rails_helper'.

这篇关于在Rails中使用RSpec和Capybara时未定义的方法'visit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆