【问题标题】:Why does an rspec feature spec with javascript not do test teardown?为什么带有 javascript 的 rspec 功能规范不进行测试拆解?
【发布时间】:2014-09-13 13:48:20
【问题描述】:

我有几个使用 ui 来创建和编辑记录的非 JavaScript 规范。 当我运行这些规范时,每个测试的 rspec 拆解会自动为我删除测试数据库记录。

但是,下面的测试是第一个使用 :js => true 处理某些 ajax 内容的测试,之后并没有删除记录,然后测试开始中断,因为数据库在启动时不再正确为空。链接和组行仍然存在于测试数据库中。

# spec/features/verifying_link_spec.rb 
require 'spec_helper'

describe "verification", :js => true, :type => :feature do

  before :all do
    User.create(:username => 'r@google.com', :password => 'esceptio')
  end 

  before :each do
    visit '/ladmin/login'
    fill_in 'username', :with => 'r@google.com'
    fill_in 'password', :with => 'esceptio'
    find('input[value="Login"]').click
  end 

  it "lets me verify a link" do
    find('div#side div a', text: 'New Group').click
    fill_in 'group[group_name]', with: 'Group Add'
    click_button 'Save'
    find('div#side div a', text: 'New Link').click
    fill_in 'link[url_address]', with: 'http://www.a.com/newtest9876link'
    fill_in 'link[alt_text]', with: 'abcd9876'
    click_button 'Save'
    this_year=Time.now.strftime('%Y')
    l=Link.first
    l.update_attribute(:verified_date, nil)
    expect(Link.count).to eq 1
    visit links_path
    find('a', text: "verify")
    click_link("verify", match: :first)
    sleep(3)
    expect(page).to have_content(this_year)
  end 

end

现在我正在使用使用 ui 删除记录的变通解决方案(如下),但这不是必需的

    # added at bottom of spec
    click_link('Details')
    click_link('Delete')
    page.driver.browser.switch_to.alert.accept
    click_link('Groups')
    click_link('Delete')
    page.driver.browser.switch_to.alert.accept

我的其他单元测试或功能测试(除了这个带有 js 的测试)都没有这个问题。它们都会创建自动删除的记录。

【问题讨论】:

  • 您在使用 database_cleaner gem 吗?你用什么来运行 JS 测试,selenium,phantomjs,chromedriver?
  • 我没有使用数据库清理器 gem。我正在考虑。尽管我想知道为什么这个测试有这个问题(而其他测试没有),但我所做的大部分是为了学习。我还对在每次测试之间截断数据库的策略以及是否会对性能产生任何影响感到担忧。我正在使用 RSpec 和 Capybara,我相信它正在使用硒

标签: ruby-on-rails ruby ajax ruby-on-rails-4 rspec


【解决方案1】:

我强烈建议在测试之间使用database_cleaner gem 来清理您的数据库。 Rspec 将所有内容包装在事务中,并在示例完成后将其回滚。但是,当您开始使用 javascript 时,您可能会将数据保存在 rspec 事务之外,然后数据库永远不会恢复到其原始状态。

截断比事务策略慢。但是,您可能只需要 JS 测试的截断策略。您可以按照本指南以这种方式设置数据库清理器 gem:http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

如果您发现您的测试花费了很多时间,您可能需要花一些时间研究一下预加载您的环境的 gem。我强烈推荐zeus

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多