RSpec的为什么不等待previous命令完成? [英] Why rspec doesn't wait completion of previous command?

查看:290
本文介绍了RSpec的为什么不等待previous命令完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Rails应用程序内包含使用水豚骚灵(使用phantomjs)RSpec的检验。前端是Backbone.js的。一些测试有时会通过,有时没有。调试表明,正确的内容是不是在检查时加载。如果我添加一个延迟,E.C.睡眠(inspection_time = 1),则该测试通过

My rails-app contain rspec-tests using Capybara poltergeist (which uses phantomjs). Frontend is Backbone.js. Some tests are sometimes passed, sometimes not. Debugging shows that the right content is not loaded at the time of inspection. If i add an delay, e.c. sleep (inspection_time = 1), then the test passes.

  describe 'Direct visit page / # / desk / page / [ID]' do
    before {sign_in user}
    it 'should contain page title' do
      visit "/#/desk/pages/#{page.id}"
      #sleep (inspection_time = 1)
      expect (page) .to have_css (". page-detail p.title",: text => page.title)
    end
  end

JS-测试执行,这么久了,还没有可靠的延迟增加了执行时间。有司机骚灵一个很好​​的解决方案(宝石捉弄人的鬼','1.6.0')?

js-tests are performed and so long, and yet unreliable delay increases execution time. Is there a good solution for drivers poltergeist (gem 'poltergeist', '1.6.0')?

也许值得赞成放弃捉弄人的鬼,例如水豚,WebKit的?

Might be worth abandon poltergeist in favor of, for example capybara-webkit?

推荐答案

测试与水豚异步请求可能会非常棘手。正如你看到水豚不知道等待异步请求的完成。但好消息是,匹配器有足够的智慧来等待的东西出现。

Testing asynchronous requests with capybara can be tricky. As you noticed capybara does not know to wait for a async request to finish. But the good news is that matchers are smart enough to wait for something to appear.

例如,如果你调用

expect(page).to have_css(".new_data")

和与NEW_DATA类的div最初不存在水豚将等待X秒(在配置中定义),看看是否出现了。然而,这有时是不够的,你可能对付某种形式的竞争状态。 Thoughtbot写了一篇关于如何解决一个很好的文章

And a div with new_data class does not initially exist capybara will wait for X seconds (defined in configuration) to see if it appears. However this is sometimes not enough, and you are probably dealing with some kind of a race condition. Thoughtbot wrote a good article on how to resolve this.

这篇关于RSpec的为什么不等待previous命令完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆