Angularjs 同时测试解析承诺和$http请求时出错
我正在尝试测试在调用http之前等待承诺的代码: 代码: 测试: 错误:Angularjs 同时测试解析承诺和$http请求时出错,angularjs,unit-testing,http,parse-platform,promise,Angularjs,Unit Testing,Http,Parse Platform,Promise,我正在尝试测试在调用http之前等待承诺的代码: 代码: 测试: 错误: 1) gets foo No pending request to flush ! 我猜是因为Parse.Promise延迟了Promise解析,并且在调用$httpBackend.flush时没有发出http请求 是否有解决方法?默认情况下 您可以通过调用Parse.Promise.disablePlusCompliant()禁用此行为在测试之前。摩卡实际上有承诺语法支持,您可以直接测试承诺,方法是
1) gets
foo
No pending request to flush !
我猜是因为Parse.Promise
延迟了Promise解析,并且在调用$httpBackend.flush
时没有发出http请求
是否有解决方法?默认情况下
您可以通过调用
Parse.Promise.disablePlusCompliant()禁用此行为代码>在测试之前。摩卡实际上有承诺语法支持,您可以直接测试承诺,方法是删除done
参数并返回承诺:
describe('foo', function() {
it('gets', function() { // no `done`
try {
$httpBackend.expect('GET', '/bar').respond(200);
return foo().then(function(res) { // return here
expect(res.status).to.be.equal(200);
});
} finally {
$httpBackend.flush();
}
});
});
这将破坏代码的执行顺序,您可能会遇到无法解释的失败。您可以仅在需要时为单个测试禁用它。这是我在搜索数小时后让它工作的唯一方法。但是你必须调用$httpBackend.flush
,才能得到$http.get
的解决承诺。你什么时候会这么做?@gfpacheco哦,对了,既然你在嘲笑httpBackend,那你就应该这么叫,让我修改一下
1) gets
foo
No pending request to flush !
describe('foo', function() {
it('gets', function() { // no `done`
try {
$httpBackend.expect('GET', '/bar').respond(200);
return foo().then(function(res) { // return here
expect(res.status).to.be.equal(200);
});
} finally {
$httpBackend.flush();
}
});
});