【问题标题】:Rspec / Capybara non-determinismRspec / Capybara 非确定性
【发布时间】:2015-05-05 22:12:13
【问题描述】:

我有一个 Rspec 测试,代码如下:

visit root_path(cc: coupon.code)

using_wait_time 5 do
  page.should have_content 'Welcome!  You have applied coupon ' + coupon.code
end

基本上,用户可以使用优惠券代码点击我们的根路径,该代码应设置在 cookie 中并通过视觉反馈进行确认。

如果我自己运行,这个测试就可以工作:

Finished in 36.81 seconds (files took 6.69 seconds to load)
2 examples, 0 failures

但如果我将它作为我整个测试套件的一部分运行,我会收到错误:

  1) Setup sets coupon
     Failure/Error: page.should have_content 'Welcome!  You have applied coupon ' + coupon.code
       expected #has_content?("Welcome!  You have applied coupon foo") to return true, got false
     # ./spec/requests/setup_spec.rb:29:in `block (3 levels) in <top (required)>'
     # ./spec/requests/setup_spec.rb:28:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:87:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:86:in `block (2 levels) in <top (required)>'

唯一可能发生的情况是 cookie 未设置。有谁知道为什么单独运行测试与将其作为整个套件的一部分运行会有所不同?

【问题讨论】:

    标签: ruby-on-rails cookies rspec capybara


    【解决方案1】:

    尝试在测试之前将其放在某个地方(就在您访问之前,甚至在后台块中):

    page.driver.browser.manage.delete_all_cookies
    

    这通常在测试之间调用,但这应该有助于进一步诊断您的规范发生了什么。

    【讨论】:

    • 我尝试使用 show_me_the_cookies gem,但没有任何效果。请注意,使用 browser.manage 仅适用于 Selenium。如果您使用的是 webkit,则会引发错误。
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多