实习生AJAX单元测试 [英] Intern AJAX Unit Tests

查看:337
本文介绍了实习生AJAX单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与如何实习生运行测试,并试图测试使用AJAX使用jQuery调用单位遇到了问题。由于实习生提供的测试客户端的webdriver的服务器(在这种情况下 phantomjs --webdriver = 8910 ),我结束了对本地主机上运行的测试: 9000 。这显然​​会导致同源策略的问题。

I've run into a problem with how Intern runs tests, and attempting to test units that use AJAX calls with jQuery. Since Intern serves the test client to the WebDriver server (In this case phantomjs --webdriver=8910), I end up with tests running on localhost:9000. This obviously causes Same-Origin Policy problems.

我可以解决这个问题,使跨域请求,但一旦身份验证Cookie是必需的,我阻止,因为对本地主机显然,我们的后端没有设置cookie,所以我要这些cookie没有访问权限。

I can get around this and make cross-domain requests, but once an authentication cookie is required I'm blocked because obviously our backend is not setting cookies for localhost, so I have no access to these cookies.

我试着改变 proxyUrl 选项指向我们的服务器在几个不同的排列,但至今没有奏效:

I've tried changing the proxyUrl option to point to our server in several different permutations, but so far nothing has worked:

https://my.server.com/
https://my.server.com/js/
https://my.server.com/js/intern/
https://my.server.com/js/intern/client.html
https://my.server.com/js/intern/client.html?config=tests/intern

看着我注意到,实习生告诉服务器加载webdriver的日志的http://本地主机:9000 / __实习生/ client.html配置= ... 。我试图然后设置 proxyUrl https://my.server.com/js/ 和符号链接 __实习生实习生,但无论是测试没有运行或者他们没有被报告给 runner.js 。当我运行这种方式的PhantomJS日志是这个样子:

Watching the WebDriver logs I've noticed that Intern tells the server to load http://localhost:9000/__intern/client.html?config=.... I've attempted to then set proxyUrl to https://my.server.com/js/ and symlink __intern to intern, but either the tests are not being run or they aren't being reported back to runner.js. The PhantomJS logs when I run this way look something like this:

[DEBUG - 2013-05-24T15:17:50.930Z] SessionReqHand - _postUrlCommand - Session '194ff0a0-c485-11e2-9c64-9986b0c95ae1' is about to load URL: https://my.server.com/js/__intern/client.html?config=tests%2Fintern&sessionId=194ff0a0-c485-11e2-9c64-9986b0c95ae1&reporters=webdriver
[DEBUG - 2013-05-24T15:17:50.943Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2013-05-24T15:17:50.962Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - _oneShotCallback - onLoadFinished
[DEBUG - 2013-05-24T15:17:50.962Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - _execFuncAndWaitForLoadDecorator - onLoadFinished: success
[DEBUG - 2013-05-24T15:17:51.051Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: false
[DEBUG - 2013-05-24T15:17:51.051Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - _resetOneShotCallbacksDecorator
[DEBUG - 2013-05-24T15:17:51.052Z] SessionReqHand - _SuccessHandler - status: success
[DEBUG - 2013-05-24T15:17:51.184Z] Session [194ff0a0-c485-11e2-9c64-9986b0c95ae1] - page.onConsoleMessage - Tests complete

测试运行,然后挂起,但是没有收到测试的结果,但是,由于 proxyUrl 不是指着实习生运行。

The test runner then hangs and never receives the results of the tests, however, since the proxyUrl is not pointed at the proxy server that Intern runs.

修改5/28

值得一提的是,有很多这可以通过后台修改规避(虽然理想的测试栈应该是可配置,以适应code的基础上,而不是强迫code基符合),但我们只能说,在这种情况下,这不是一个选项。

It's worth mentioning that a lot of this can be circumvented through back end modifications (though ideally a test stack should be configurable to fit the code base, rather than forcing the code base to conform), but let's just say that in this case that's not an option.

理想我想实习是可配置的加载 client.html 从指定的URL,然后让记者邮寄到本地主机:通过一些跨域兼容的方式,像JSONP 9000 代理。我找不到任何其他合理的替代性,因此它看起来像此刻实习生根本不完全支持通过AJAX后端进行通信的单元测试。

Ideally I would want Intern to be configurable to load client.html from a specified URL, and then have the reporter POST to the localhost:9000 proxy via some cross-origin-compatible means, like JSONP. I can't find any other reasonable alternative, so it looks like at the moment Intern simply does not fully support unit tests that communicate with a back end through AJAX.

推荐答案

当你改变的URL实习生代理指向另一个服务器上,它需要实际上是代理回运行实习生服务器的服务器在本地主机:9000 。它不能只是一个符号链接实习生目录中一个普通的Web服务器。

When you are changing the URL to the Intern proxy to point to another server, it needs to be a server that is actually proxying back to the Intern server that runs at localhost:9000. It can’t just be a plain Web server with a symlink to the Intern directory.

这样做是一样的(CouchDB的,阿帕奇,Node.js的,等等)的任何其他种类的代理到另一个后端。对于nginx的,这意味着使用 HttpProxyModule proxy_pass的http://本地主机:9000 /; 。对于Apache,的mod_proxy 的ProxyPass / HTTP: //本地主机:9000 / 。请记住,如果你不允许实习生服务器来服务你的JavaScript(即你只代理 __实习生),您将不会收到code覆盖数据,所以最好是代理以外的所有服务的网址,你正在尝试测试。

Doing this is the same as any other sort of proxying to another backend (CouchDB, Apache, Node.js, whatever). For nginx, it means using the HttpProxyModule to proxy_pass http://localhost:9000/;. For Apache, mod_proxy and ProxyPass / http://localhost:9000/. Keep in mind that if you do not allow the Intern server to serve your JavaScript (i.e. you only proxy to __intern), you will not receive code coverage data, so it is best to proxy everything except the service URLs you are trying to test.

在代理到实习生来测试Web服务的详细完整的信息可以在wiki页面的使用实习生进行单元测试Ajax调用

More complete information on proxying to Intern to test Web services can be found on the wiki page Using Intern to unit test Ajax calls.

这篇关于实习生AJAX单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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