Rspec / Capybara非决定论

时间:2015-03-04 18:59:58

标签: ruby-on-rails cookies rspec capybara

我有一个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没有设置。有谁知道为什么自己运行测试与运行它作为整个套件的一部分会产生影响?

1 个答案:

答案 0 :(得分:0)

尝试在测试之前将其放在某处(在您访问之前,或者甚至在背景块中):

page.driver.browser.manage.delete_all_cookies

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

相关问题