简体   繁体   English

ajax调用在rspec测试中失败

[英]ajax calls failing in rspec tests

We have a very large application that we are currently implementing rspec feature tests. 我们有一个非常大的应用程序,目前正在实施rspec功能测试。 It is a rails application with a knockout.js front-end framework. 这是一个带有kickout.js前端框架的Rails应用程序。 We have a lot of trouble with wait_for_ajax . 我们在使用wait_for_ajax时遇到了很多麻烦。 We constantly have tests fail because of ajax, and our developers sometimes have to put wait_for_ajax in sometimes three or four times. 由于ajax,我们经常会导致测试失败,有时我们的开发人员有时不得不将wait_for_ajax放入三到四次。

This can't be the correct way to do this. 这不是执行此操作的正确方法。 What is the correct way to wait for ajax calls in rspec tests? 在rspec测试中等待ajax调用的正确方法是什么?

The hard truth is that javascript tests in capybara are painful and slow. 硬道理是,水豚中的javascript测试既痛苦又缓慢。

The only way we can determining if an ajax request if the ajax requests are finished is though hacks like this: 我们可以确定ajax请求是否完成的唯一方法是通过如下方式进行黑客攻击:

module JavascriptTestHelpers
  def wait_for_ajax
    Timeout.timeout(Capybara.default_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

  def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
  end
end

I would be really happy if someone proves me wrong. 如果有人证明我错了,我会很高兴。 Its almost impossible to get a handle on a specific ajax request unless you do a crazy hack like assigning the promises to the global object. 除非您进行疯狂的黑客攻击,例如将promise分配给全局对象,否则几乎不可能获得特定ajax请求的句柄。 In general this seems to be problematic no matter what the language when automating web browsers. 总的来说,无论使用哪种语言使Web浏览器自动化,这似乎都是有问题的。

Running tests in parallel can help a bit with the slowness. 并行运行测试可以对速度有所帮助。

Thoughbot has a really good blog post on some of the common gotchas of capybara JS test which can cause "flapping" tests. 不过,关于水豚JS测试的一些常见问题,bot有一个非常不错的博客文章 ,这可能会导致“拍打”测试。

I think that for client heavy applications a javascript test suite in Mocha, Jasmine or (shudder) QUnit is a necessary compliment. 我认为对于客户端繁重的应用程序来说,Mocha,Jasmine或(抖动)QUnit中的javascript测试套件是必要的补充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM