Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 同时测试解析承诺和$http请求时出错_Angularjs_Unit Testing_Http_Parse Platform_Promise - Fatal编程技术网

Angularjs 同时测试解析承诺和$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()禁用此行为在测试之前。摩卡实际上有承诺语法支持,您可以直接测试承诺,方法是

我正在尝试测试在调用http之前等待承诺的代码:

代码:

测试:

错误:

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();
    }
  });
});