简体   繁体   English

Stub Rails UJS / Ajax响应状态以测试Rspec / Capybara功能测试中的返回消息

[英]Stub Rails UJS/Ajax responses status to test returning message in Rspec/Capybara feature test

I have AJAX calls initiated by Rails UJS that I would like to test. 我有要测试的由Rails UJS发起的AJAX调用。 specifically, I have used Rails UJS ajax events to provide for cases of errors. 具体来说,我已经使用Rails UJS ajax事件来提供错误情况。

I would like to test them but I don't know how to tell rspec/capybara to "stub" and assume the error code 我想测试他们,但我不知道怎么告诉rspec的/水豚为“存根”,并承担错误代码

$("button").
      on('ajax:error',function(event,xhr, status, error){
          if(status == "timeout") {
            var msg;
            msg = Messenger().post({
              message:    "This is taking too long"
            });          
          } else {
            var msg;
            msg = Messenger().post({
              message:    "it seems there is a bug. Please try again."
            });
          };
        });

I would like to do something like the following: 我想做以下事情:

describe "test returning error message", js: true do
   it "should be successful" do        
        visit deal_page_path(deal)
        first('a.button').click 
        stub(:xhr.status) = "timeout"           
        expect(page).to have_content('This is taking too long') 
      end
end

How to do this? 这个怎么做?

Note: the ajax requests are internal they don't go to third party API or services (such as facebook for ex). 注意:ajax请求是内部的,它们不会访问第三方API或服务(例如ex的facebook)。

When testing with Capybara (JS enabled drivers) it has no access to the request or response except through the changes it creates in the browser. 使用Capybara(启用JS的驱动程序)进行测试时,它无法访问请求或响应,除非通过在浏览器中创建的更改来访问。 You could build a test mode into your relevant controllers that could be turned on and off to allow it to output the errors you want, but the cleanest way to do this is probably to use a programmable proxy like puffing-billy which will allow you to selectively return whatever you'd like for any given request from the browser. 您可以在相关的控制器中构建测试模式,可以对其进行打开和关闭以使其输出所需的错误,但是最简单的方法可能是使用像puffing-billy这样的可编程代理,这将使您能够有选择地从浏览器返回任何给定请求所需的内容。 One thing to realize is that this isn't testing that app correctly returns errors, it's just testing that your front-end handles errors the way you expect. 要意识到的一件事是,这并不是在测试应用程序是否正确返回错误,而只是在测试您的前端以您期望的方式处理错误。

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

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