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

2024-04-27

我无法让水豚与 rspec 一起工作。它给了我这个错误:

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

我知道有很多关于此问题的帖子,但没有一个解决方案适合我。其中大多数涉及的规格不在 /spec/features 中 - 而我的则在 /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 Helper '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

我的宝石文件:

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。无论哪种方式都没有区别。有什么想法我接下来应该尝试吗?


尝试添加:

  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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Rails 中使用 RSpec 和 Capybara 时未定义的方法“visit” 的相关文章

随机推荐

  • 如何在代码中设置控件模板?

    我在 XAML 中有这个
  • 在自定义 Flex 组件中绘制叠加层

    如何在 Flex 中创建一个自定义 MXML 组件 该组件基于现有组件 但在某些情况下会在该现有组件上绘制覆盖层 理想情况下 新组件应该基于 派生自 现有组件 以便现有组件的出现可以用新组件替换 我尝试在新组件中重写 updateDispl
  • Selenium + Python如何监听元素的变化

    这个想法是创建一个机器人来读取聊天消息 所有消息都在一个ul gt li 不必写消息 例如 ul class message list li class message Hello There li li class message Hel
  • C++ 中的映射的多个键

    我有一个表 其中的条目是这样的 Row Column1 Column2 Column3 Column4 1 0X0A 1 2 A 2 0X0B 2 2 B 3 0x0C 3 2 C 现在我想使用映射 以便我可以使用第 1 列或第 2 列作为
  • 如何使用 Apache Beam 从 Google Pub/Sub 访问消息 ID?

    我一直在 Python 2 7 16 上使用 2 13 0 SDK 测试 Apache Beam 以流模式从 Google Pub Sub 订阅中提取简单消息 并写入 Google Big Query 表 作为此操作的一部分 我尝试使用 P
  • 使用 PIL 对图像进行着色,同时保持透明度?

    好吧 情况是这样的 我想使用 Python 图像库来 主题化 图像 如下所示 Theme color 33B5E5 IN OUT 我使用 ImageMagick 命令得到了结果 convert image png colorspace gr
  • 滚动到 HTML 网站中的顶部 JavaScript

    我正在尝试在我的网站中实现滚动到顶部功能 www arrow tvseries com 网站上可以看到 按钮 但它无法正常工作 因为单击时它不会滚动到页面顶部 更重要的是 我希望 滚动到顶部按钮 在向下滚动 例如一半页面 时可见 这是 Ja
  • Discord.js - guildMemberRemove 不起作用,guildMemberAdd 工作得很好

    抱歉 如果格式不正确 我以前从未在这里写过问题 首先 这是我第一次用 JavaScript 编写任何东西 所以这可能是我犯的一些愚蠢的错误 导致了我的问题 我想要做的是当成员加入服务器时发送一条消息 并在成员离开时发送不同的消息 当有人加入
  • 以编程方式最大化屏幕一半的窗口

    我想最大化屏幕左侧的随机窗口 我可以在我的代码中使用 Windows Aero 函数吗 这个窗口can像用鼠标一样最大化 我只想以编程方式做到这一点 I use C 我可以得到IntPtr窗户的 如果可能的话 不要伪造鼠标或键盘输入 这可以
  • Attachconsole的问题

    我需要使 Windows GUI 应用程序可以在控制台中运行 因此当使用命令行调用应用程序时 我将控制台附加到进程 但是 应用程序退出后 除非按 ENTER 键 否则不会显示带有路径的控制台提示符 有什么办法可以不按回车键直接显示路径提示吗
  • 通过从 .BAT 中查找进程正在使用的端口来终止进程

    在 Windows 中 什么可以查找端口 8080 并尝试通过 BAT 文件终止它正在使用的进程 打开命令提示符并运行以下命令 C Users username gt netstat o n a findstr 0 0 3000 TCP 0
  • 如何在命名管道 (mkfifo) 上执行非阻塞 fopen?

    如果我有一个程序使用 mkfifo 创建并尝试打开命名管道 如何在不阻塞的情况下打开管道进行读取或写入 具体来说 我正在编写一个 C 程序 它可以在有或没有 GUI 的情况下运行 用 Java 编写 在 C 程序中 我使用 mkfifo 成
  • 如何使用 Windows 粘贴命令将文本粘贴到 C# 中的其他应用程序?

    如何调用互操作来使用 Windows Pastse 命令将文本粘贴到 C 中的其他应用程序 调用互操作 我的意思是如何对 C 进行编程相同的右键单击粘贴文本 在某些情况下这可能有点棘手 但实际上非常简单且容易做到 下面的示例介绍了如何使用文
  • tensorflow SavedModel - 如何迭代保存

    我正在采用新的SavedModel据我所知 API 是 未来 应该优先于tf train Saver 我想要实现的目标是每次保存一个模型N批次数 我想最多保留 20 个已保存的模型 显然我可以自己监控这一点 但如果tf train Save
  • CodeIgniter 中使用 Active Record 的查询中的 DATE_FORMAT 不起作用

    编码员 我在这里遇到一个小问题 找不到解决方案 我正在使用 CI 的 Active Record 构建查询 这是查询的代码 this gt db gt select u id AS user id u email p display nam
  • 用于查找艺术家属性的 dbpedia SPARQL 查询

    我试图通过 DBPedia 和 SPARQL 查询语言获取有关艺术家的详细信息 但是 根据我的理解 如何获取某些信息似乎几乎是不可能的 我正在尝试找到一位艺术家并获取诸如他们的家乡之类的信息 我猜查询应该类似于 SELECT c WHERE
  • 3 维装箱算法

    我面临着 3 维装箱问题 目前正在进行一些初步研究 了解哪些算法 启发式方法目前能产生最佳结果 由于问题是 NP 难问题 我不希望在每种情况下都能找到最佳解决方案 但我想知道 1 最好的精确求解器是什么 分支定界 我期望使用合理的计算资源可
  • QT 5.6 QWebEngine不保存cookie

    我正在创建名为 webengine 的简单 QT 应用程序 pWebView new QWebEngineView this pWebView gt load QUrl http technoz ru pWebView gt show On
  • jni.h:没有这样的文件或目录

    我一直在关注本教程 http www java tips org other api tips jni simple example of using the java native interface html 在第 5 步 我从 GCC
  • 在 Rails 中使用 RSpec 和 Capybara 时未定义的方法“visit”

    我无法让水豚与 rspec 一起工作 它给了我这个错误 undefined method visit for