为了能够测试一个需要对服务器发出异步请求的函数,我使用qunit在我的gruntfile.js中使用了以下asyncTest调用。
asyncTest("country", function() {
Catalog.Country.load( function() {
ok( Utils.objectsize( Catalog.Country.list)>0, "country list has content");
equal( Catalog.Country.getCountryNameFromCode('US'), 'USA', "USA for code US exists");
equal( Utils.objectsize( Catalog.Country.getStatesForCountry('US')), 51, "USA has 51 states");
equal( Utils.objectsize( Catalog.Country.getStatesForCountry('US', 1)), 51, "USA has 51 states (with keys)");
start();
});
});
此代码来自我的测试HTML文件,该文件在浏览器上运行正常。所有4个断言都没问题。但是使用grunt和这个简单的qunit块:
qunit: {
main: [ 'tests/country.html' ]
}
以下面的调试输出结束:
[D] ["phantomjs","qunit.begin"]
[D] ["phantomjs","onLoadFinished","success"]
[D] ["phantomjs","qunit.moduleStart",null]
[D] ["phantomjs","qunit.testStart","country"]
[D] ["phantomjs","onResourceRequested",{"headers":[{"name":"Origin","value":"file://"},{"name":"User-Agent","value":"Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34"},{"name":"Accept","value":"application/json, text/javascript, */*; q=0.01"}],"id":5,"method":"GET","time":"2014-03-14T14:54:47.201Z","url":"http://localhost:1021/json.fly?a=country&lang=en"}]
[D] ["phantomjs","onResourceReceived",{"contentType":"application/json","headers":[{"name":"Date","value":"Fri, 14 Mar 2014 14:54:47 GMT"},{"name":"Server","value":"Apache/2.2.22 (Ubuntu)"},{"name":"Set-Cookie","value":"ossess=localhost_AUA_AT_de_0C1ECE3755F7367CF48F207C53E0DB61; path=/; HttpOnly"},{"name":"Access-Control-Allow-Origin","value":"*"},{"name":"Transfer-Encoding","value":"chunked"},{"name":"Content-Type","value":"application/json"},{"name":"X-Cache","value":"MISS from proxy"},{"name":"X-Cache-Lookup","value":"MISS from proxy:3128"},{"name":"Via","value":"1.0 proxy (squid/3.0.STABLE25)"},{"name":"Proxy-Connection","value":"close"}],"id":5,"redirectURL":null,"stage":"end","status":200,"statusText":"OK","time":"2014-03-14T14:54:47.348Z","url":"http://localhost:1021/json.fly?a=country&lang=en"}]
[D] ["phantomjs","fail.timeout"]
>> PhantomJS timed out, possibly due to a missing QUnit start() call.
Warning: 1/1 assertions failed (0ms) Use --force to continue.
我在json API上放了一些日志,请求真的进来了,并返回了json响应。正如我在浏览器上说的那样有效。但不是在控制台与咕噜声。 这有什么不对?即使请求成功,我的Catalog.Country.load函数的jquery的ajax调用的成功处理程序也不会被调用,因为你可以在调试输出中看到,它显示来自apache的STATUS:200和我在后端的日志也是显示返回的数据。以下是处理回调的Catalog.Country.load函数的内容。
load: function(callback) {
$.ajax({
dataType: 'json',
url: 'http://localhost:1021/json.fly?a=country&lang=en',
success: $.proxy( function( data) {
this.list = data; // store result in local list var
if( callback && callback.call) {
callback.call(this); // call the callback function to inform about success
}
}, this)
});
}
更新 我更新了gruntfile中的qunit块以禁用phantomjs的web-security,所以它允许跨域,我希望这是原因,但仍然存在问题:
qunit: {
options : {
'--web-security': false,
'--local-to-remote-url-access': true
},
main: [ 'tests/country.html' ]
}
重要更新: 您看到正在使用localhost但我使用的是某个域,因此它是跨域的。工作如上所述,将其更改为localhost。但我仍然需要使用远程域进行测试或至少使用相同的域但另一个端口。所以在qunit中使用web-security选项仍然没有帮助,似乎phantomjs忽略了跨域设置web-security和local-to-remote-url-access。为什么?