rspec 中的水豚和 before(:all)

2024-03-01

这是我的完整规格:

require 'spec_helper'

describe "Idea page", js: true do

  subject { page }

  before(:all) do
    create(:idea)
    visit root_path
    click_link "Log In"
  end

  context "Single idea" do
    before do
      page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
    end

    it { should have_selector('a',text:'Claim') }
    it "should have a button for reporting the idea"
    it "should have a button for opening all links"

    describe "Claiming" do
      before do
        click_link "Claim"
      end
      it {should have_selector('a', text:'Claimed')}
    end

  end
end

Without (:all)(即,当它只是before在第一个块中),浏览器打开,单击“登录”链接,转到右侧页面,然后单击该链接。好的。

但在尝试单击第二个链接(“索赔”)之前,它会再次执行此操作,这既耗时又容易出错。所以我尝试用以下方法解决这个问题before(:all).

但现在,它只是弹出打开 Firefox,等待片刻,然后再次关闭它,而不执行任何操作。测试失败说:

Failures:

  1) Idea page Single idea 
     Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
     Capybara::ElementNotFound:
       Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a"
     # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>'

  2) Idea page Single idea Claiming 
     Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
     Capybara::ElementNotFound:
       Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a"
     # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>'

显然,因为浏览器页面是空白的。

我缺少什么?谢谢。

Edit:也许有一些基本的东西我不明白。和before(:each)这是测试尝试执行的操作:

1) 登录网络应用程序,确保有“声明”按钮。

2)登录网络应用程序again, 打开手风琴again,然后单击“声明”按钮看看会发生什么。

所以每一步的开始都是完全相同的,浏览器一次又一次地做同样的事情。事情就应该这样吗?

如果是这样,为什么我这样做时会出现错误?具体来说,与before(:each)我明白了:

Failures:

  1) Idea page Single idea Claiming 
     Failure/Error: it {should have_selector('a', text:'Claimed')}
     Selenium::WebDriver::Error::UnhandledAlertError:
       Modal dialog present
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/command_processor.js:10287:in `nsCommandProcessor.execute'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7328:in `Dispatcher.executeAs/<'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7488:in `Resource.handle'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7435:in `Dispatcher.dispatch'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:10119:in `WebDriverServer/<.handle'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1935:in `unknown'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:2261:in `ServerHandler.handleResponse'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1168:in `Connection.process'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1616:in `RequestReader._handleResponse'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1464:in `RequestReader._processBody'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1333:in `RequestReader.onInputStreamReady'
     # ./spec/features/ideas_spec.rb:26:in `block (4 levels) in <top (required)>'

即使我看到浏览器单击该按钮,将其切换为“已声明”,并且没有显示模式对话框。

Edit 2:我纠正了!那里was毕竟是一个模式对话框!我修复了 JS 以停止显示它,测试现在通过了。我仍然认为框架必须从头开始每一步重复整个序列(似乎是浪费工作),但无论如何。谢谢你!


这是因为测试数据,包括水豚会话数据(例如登录状态)和创建的模型create(:idea)在规格之间被消除。

你想和before(:each) not :all,尽管它更耗时。

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

rspec 中的水豚和 before(:all) 的相关文章

  • Rails 3 应用程序的 MySQL Cluster (NDB) 与 MySQL Replication (InnoDB):优点/缺点?

    我们正在对当前系统进行概述 试图找出是否可以提高性能和可靠性 目前 我们运行着一堆内部 Rails 应用程序和基于 Rails 的网站 有些已经是 Rails 3 有些正在转换为 Rails 3 它们都连接到以下 MySQL 设置 mysq
  • 未使用 form_with 显示时出错

    早上好 我正在按照教程进行操作http edgeguides rubyonrails org getting started html http edgeguides rubyonrails org getting started html
  • 与同一模型的多个 has_many 关系

    我有一个可以创建帖子的模型用户 User has many posts Post belongs to user 但是 我还想允许用户将帖子保存为书签 所以我添加了以下内容 Bookmark belongs to post belongs
  • Rails 4 不更新嵌套属性

    Issue 代替updating嵌套属性 它们正在created当我点击现有的嵌套属性之上 update相关的行动features controller rb 可能的原因 我认为问题在于我对 Rails 缺乏了解form for 我认为细分
  • 在所有延迟的作业之前挂钩

    是否可以在所有delayed job任务之前运行一个方法 基本上 我们试图确保每台运行delayed job的服务器都有我们代码的最新实例 因此我们希望运行一个方法来在每个作业运行之前检查这一点 我们已经有了 check 方法并在其他地方使
  • 在多个模型中与 has_many :through 建立关联

    请帮助了解该怎么做 project payments有这棵树 Project Stages Costs Payments 项目 rb has many stages has many costs through gt stages stag
  • Rails 上的 SASS 无效 CSS 错误

    我正在尝试使用http startbootstrap com stylish portfolio http startbootstrap com stylish portfolio但是 在我的 Rails 应用程序中 我在 vintage
  • 使用 PostgreSQL 的模式和 Rails 创建多租户应用程序

    我已经想通的事情 我正在学习如何在 Rails 中创建多租户应用程序 该应用程序根据用于查看应用程序的域或子域来提供来自不同模式的数据 我已经回答了一些问题 如何让 subdomain fu 也能与域一起使用 这是有人问了同样的问题 htt
  • Heroku 码头部署

    我正在关注这篇文章https devcenter heroku com articles container registry and runtime https devcenter heroku com articles containe
  • 如何访问“可以?”细胞内的方法?

    我在用着cancan and cells我的 ruby on rails 项目中的 gems 如何访问can 细胞内的方法 Thanks 我必须这样做 尝试 class MyCell lt Cell Rails include CanCan
  • 如何在 Rails 3 中查看用户的实时活动?

    我想做的是让我的管理员用户能够实时 通过一些 AJAX jQuery 功能 看到我的用户正在做什么 我该如何去做呢 我认为它与会话活动有关 并且我已经开始将会话保存到数据库 而不是 cookie 但一般来说 我如何获取该信息并实时解析它 我
  • 在 RSpec 测试期间抑制控制台输出

    我正在测试在控制台上放置一些消息的类 包含 put p 警告等 我只是想知道在 RSpec 测试期间是否有能力抑制此输出 我压抑puts通过重定向在我的类中输出 stout到一个文本文件 这样 如果我出于任何原因需要查看输出 它就在那里 但
  • ActiveRecord 查询,按关联排序,最后一个 has_many

    我试图列出所有Users by the created at最近创建的关联记录 通讯 列 到目前为止我所拥有的 User includes communications order communications created at IS
  • 限制 has_many 关联中的对象数量

    我有一个相册 里面有很多照片 counter cache 设置会更新相册表中的 photos count 列 如何限制相册的照片数量 就我而言 使用就足够了validates length of class Album has many p
  • 如何解决“找不到密钥::密码”?

    I m very铁轨已经生锈了 我认为自从我使用它以来的七年里 发生了很多变化 我正在尝试设置这个示例应用程序 https iridakos com news 2015 06 21 rails sample api ui https iri
  • Rails 3 按字段排序和最后

    您好 我对 Rails 3 2 和订购有疑问 当想要按字段对集合进行排序时 调用时 last ActiveRecord行为怪异 gt gt User order FIELD id 1 User Load 0 4ms SELECT users
  • 使用 Ruby on Rails 索引多列

    我在 Mysql 数据库中有一个表 我想在其上使用多列索引 如何在不使用 mysql 控制台的情况下在 Rails 中执行此操作 在迁移定义中 您可以这样做 add index table name column1 column2
  • Cucumber 是否不需要编写单元测试?

    我对 Ruby ROR 可用的测试框架数量之多感到有点困惑 我最近看了黄瓜轨道广播 http railscasts com episodes search cucumber并发现它们非常有趣 所以我开始玩游戏 然后努力从概念上考虑在哪里进行
  • 如何将 CarrierWave 文件迁移到新的存储机制?

    我有一个 Ruby on Rails 站点 其中包含使用 CarrierWave 进行文件处理的模型 当前使用本地存储 我想开始使用云存储 并且需要将现有的本地文件迁移到云端 我想知道是否有人可以指出这样做的方法 使用模型属性的好处是 它允
  • 载波无法删除图像

    我现在使用 Carrierwave 上传图像 一切都很好 除了一个 当我添加用于删除上传图像的复选框时 我收到错误 无法批量分配受保护的属性 remove image Form Model class Manufacturer lt Act

随机推荐