grunt-mocha - $.get 内的断言失败
grunt-mocha - assertion inside $.get fails
我正在尝试将 grunt-mocha 与 requireJS 连接起来进行一些单元测试,效果很好,但我似乎无法测试 ajax 调用。
我有一个代码库:https://bitbucket.org/IamHttP/grunt-mocha-tests/overview
具体文件可以找到here
代码本身粘贴在这里:
describe('testJqueryAjax', function(){
it('fetch data from an xhr', function(done){
this.timeout(3000);
require(['jquery'],function($){
$.ajax({
url: 'https://api.github.com',
success: function(str){
try{
chai.assert.equal( 1 , 1 );
done();
}
catch(e){
done(e);
}
}
}).fail(function(){
try{
chai.assert.ok( false );
done();
}
catch(e){
done(e);
}
});
});
});
});
问题:
ajax 请求总是失败,无论我将其指向何处。 .fail
函数总是被调用。
我猜这是 phantomJS 不能很好地与 jQuery 的 $.ajax 调用一起工作的问题,但我无法弄清楚。
感谢任何帮助!
通过向 phantomJS 添加配置选项解决了该问题。
这是使用 mocha 的 grunt 任务完成的:
mocha : {
all : {
src : ['tests/testrunner.html']
},
options : {
run: true,
page: {
settings: {
webSecurityEnabled: false, // disable cors checks in phantomjs
},
},
},
注意 webSecurityEnabled 选项。
我正在尝试将 grunt-mocha 与 requireJS 连接起来进行一些单元测试,效果很好,但我似乎无法测试 ajax 调用。
我有一个代码库:https://bitbucket.org/IamHttP/grunt-mocha-tests/overview
具体文件可以找到here
代码本身粘贴在这里:
describe('testJqueryAjax', function(){
it('fetch data from an xhr', function(done){
this.timeout(3000);
require(['jquery'],function($){
$.ajax({
url: 'https://api.github.com',
success: function(str){
try{
chai.assert.equal( 1 , 1 );
done();
}
catch(e){
done(e);
}
}
}).fail(function(){
try{
chai.assert.ok( false );
done();
}
catch(e){
done(e);
}
});
});
});
});
问题:
ajax 请求总是失败,无论我将其指向何处。 .fail
函数总是被调用。
我猜这是 phantomJS 不能很好地与 jQuery 的 $.ajax 调用一起工作的问题,但我无法弄清楚。
感谢任何帮助!
通过向 phantomJS 添加配置选项解决了该问题。
这是使用 mocha 的 grunt 任务完成的:
mocha : {
all : {
src : ['tests/testrunner.html']
},
options : {
run: true,
page: {
settings: {
webSecurityEnabled: false, // disable cors checks in phantomjs
},
},
},
注意 webSecurityEnabled 选项。