【问题标题】:Feature spec breaking when turning on js: true - Capybara - Rails打开 js 时功能规范中断:true - Capybara - Rails
【发布时间】:2016-11-01 02:03:36
【问题描述】:

我的应用程序有一些与 capybara 相关的功能规格。现在我有一个我正在尝试测试的模态。模态使用Javascript。但是当我运行我的规格时。我得到这个非常奇怪的错误。在这里。

错误

Failure/Error: page.driver.browser.set_cookie("account_id=#{account_id}")

 TypeError:
   no implicit conversion of Symbol into Integer
 # /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `[]'
 # /Users/intern/.rvm/gems/ruby-2.3.1/gems/poltergeist-1.11.0/lib/capybara/poltergeist/browser.rb:313:in `set_cookie'
 # ./spec/support/_request_macros.rb:20:in `set_current_account'
 # ./spec/features/manage_accounts_spec.rb:101:in `block (3 levels) in <top (required)>'
 # /Users/intern/.rvm/gems/ruby-2.3.1/gems/rspec-wait-0.0.9/lib/rspec/wait.rb:46:in `block (2 levels) in <top (required)>'

这是正在破坏的文件

def set_current_account(account_id)
 page.driver.browser.set_cookie("account_id=#{account_id}")
end

这是我的测试

测试

 it "redirects to the previous account if a user accesses an account they are not on" do
  third_account = create(:account, name: "Third Account", users: [user])

  set_current_account(third_account.id)
  visit root_path
  expect(current_account).to eq(third_account.id)

  user.accounts.delete(account1)
  click_on "Test Account"

  expect(current_path).to eq(snitches_path)
  expect(page).to have_content("You don't have access to that account!")
  expect(current_account).to eq(third_account.id)
  expect(page).to have_content("Third Account")
end

我在上面的上下文中有 js:true。

我真的不知道出了什么问题。希望你们中的一些人更熟悉水豚和 javascript 测试。让我知道你的想法

【问题讨论】:

    标签: javascript ruby-on-rails ruby capybara


    【解决方案1】:

    通过查看您似乎正在使用的 poltergeist - https://github.com/teampoltergeist/poltergeist/blob/28cb0d6fd427424acaddc4800514c13efedcb199/lib/capybara/poltergeist/browser.rb 传递的 cookie 需要是一个哈希 - 实际上您不应该在 page.driver.browser 上调用任何东西(这意味着 99% 的时间你正在做你不应该做的事情)。您可以在 page.driver - https://github.com/teampoltergeist/poltergeist/blob/master/lib/capybara/poltergeist/driver.rb 上调用 set_cookie - 这需要 (name, value, options = {}) 所以

    page.driver.set_cookie("account_id", account_id)
    

    这将解决您当前遇到的问题。但是,在 page.driver 上调用某些东西意味着您可能有 50% 的时间做错了。您真的不应该手动将 cookie 设置为虚假登录,而应该实际登录或将您正在使用的任何身份验证库设置为测试模式并以这种方式进行(例如,如果使用设计 - https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

    此外,您的测试还有许多问题可能导致测试不稳定。

    1. visit 不能保证在返回之前等待页面完成加载,因此紧随其后的不是基于屏幕上可见项目的任何断言都可能是不稳定的。
    2. 在支持 JS 的驱动程序中调用 current_account(假设这是一个应用程序方法),这意味着单独的线程和您的测试无法访问控制器数据,实际上并不能很好地工作 - 而只是检查可见项目页面。
    3. click_on 不会等待由它触发的操作完成,因此立即检查之后的路径将非常不稳定 - 而是使用将重试的 have_current_path 匹配器

      expect(page).to have_current_path(snitches_path)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多