ruby 在Rails中使用RSpec和Capybara时未定义方法“visit”

yv5phkfx  于 2023-08-04  发布在  Ruby
关注(0)|答案(7)|浏览(114)

我不能让水豚和rspec一起工作。它给了我这个错误:

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

字符串
我知道有很多关于这个问题的帖子,但没有一个解决方案对我有效。其中大部分都涉及到规格不在/spec/features中--而我的在其中。
首先是错误:

$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


我认为重要的是要注意,在第一次我使用的URL助手'new_sessions_path',它一直给我一个错误undefined local variable or method 'new_sessions_path'。我知道它是有效的,因为:

$ 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:

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


my_app/spec中的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 '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:

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 => :feature来定义上面的测试。不管怎样都没什么区别。你知道我下一步该做什么吗?

tkclm6bt

tkclm6bt1#

尝试添加:

config.include Capybara::DSL

字符串
到你的配置块。

# 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'

# 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

  config.include Capybara::DSL

end

yqhsw0fo

yqhsw0fo2#

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

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和水豚的例子都没有这个要求,但是哦,好吧。问题解决了。

原始答案(旧版本rspec)

require 'spec_helper'由旧版本的RSpec使用。更好的答案是require 'rails_helper'

8tntrjer

8tntrjer3#

自从水豚2.0一个必须使用文件夹规格/功能水豚命令不工作在文件夹规格/要求了。

b09cbbtk

b09cbbtk4#

尝试在before块中执行所有设置:

spec/features/security_spec.rb

describe "security" do
  before do
    visit "/sessions/new"
    fill_in "username", :with => "user"
    fill_in "password", :with => "pass"
    click_button "Sign In"
  end

  it "signs users in" do
    page.should have_content('Login Successful')
  end
end

字符串

f87krz0w

f87krz0w5#

我也有这个问题,
在我的特性顶部添加require 'rails_helper'最终解决了我的问题:

require 'rails_helper'

RSpec.describe "Products", type: :request do
 describe "GET /products" do
 it "display tasks" do
  Product.create!(:name => "samsung")
  visit products_path
  page.should have_content("samsung")
  #expect(response).to have_http_status(200)
  end
 end
end

字符串
并在rails_helper.rb中添加'config.includCapybara::DSL'

RSpec.configure do |config|

 config.fixture_path = "#{::Rails.root}/spec/fixtures"

 config.use_transactional_fixtures = true

 config.infer_spec_type_from_file_location!

 config.include Capybara::DSL

end

liwlm1x9

liwlm1x96#

除了在从旧版Rails应用程序中使用require 'spec_helper.rb'而不是require 'rails_helper.rb'升级时会遇到的升级问题外,发生这种情况有3个已知的原因:

1.您的spec不是“feature”类型,这意味着Capybara不知道如何使用JavaScript或浏览器运行它。你想做两件事之一:1)通常,您希望在RSpec.configure中设置config.infer_spec_type_from_file_location!,这意味着features文件夹中的内容将成为一个特性。

如果您有一些非标准内容,您可以将type: :feature添加到spec describe块中,以将spec转换为特性,但通常更简单的方法是将它们放入/features文件夹中,让推断设置完成其工作。

2.你不小心把visit放到了it块外面

访问必须在它的范围内,这是在描述。请务必不要将访问直接放在描述中。

3.其他一些你看不到的内核恐慌导致Capy关闭了该规格。

这是一个很难诊断的问题,但我已经看到了。这意味着Capy实际上并没有正确解析这个文件,所以当它到达visit块时,不知何故不在正确的作用域中。仔细挑选你的Capy规格,找出你在哪里引入它。
我今天引发了内核恐慌,但有一个let块被称为page(哎呀)。page在这里似乎是Rspec或Capy的保留字,它会导致内核恐慌,从而导致spec无法解析,从而导致找不到visit方法。
在我的例子中,它只是简单地改变了这个:

let(:page) {Page.new()}

字符串

let(:content_page) {Page.new()}


请注意,page这个词并不是Rails保留的,它可以作为数据库名称和模型名称,但是在这里使用page作为let变量名称的特定构造似乎会导致Capy变得有点糟糕。

inkz8wg9

inkz8wg97#

其中一个原因可能是如果您使用let定义了page变量:

RSpec.describe "something" do
  let(:page) { some_page }

  it do
    visit "/"
  end
end

字符串
导致以下错误:

1) something
     Failure/Error: visit "/"

     NameError:
       undefined method `visit' for class `#<Class:0x00007ffca6221898>'
       Did you mean?  visible


要修复它,请重命名page变量。

相关问题