Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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 我如何按照承诺和业力/角度模拟柴的延迟?_Angularjs_Karma Runner_Chai_Karma Mocha_Chai As Promised - Fatal编程技术网

Angularjs 我如何按照承诺和业力/角度模拟柴的延迟?

Angularjs 我如何按照承诺和业力/角度模拟柴的延迟?,angularjs,karma-runner,chai,karma-mocha,chai-as-promised,Angularjs,Karma Runner,Chai,Karma Mocha,Chai As Promised,假装延迟承诺解决的示例测试: describe('Example', function() { var $q; var $rootScope; var $scope; var $timeout; var fakePromise; beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_) { $q = _$q_; $rootScope = _$rootScope_; $timeout =

假装延迟承诺解决的示例测试:

describe('Example', function() {
  var $q;
  var $rootScope;
  var $scope;
  var $timeout;
  var fakePromise;

  beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_) {
    $q = _$q_;
    $rootScope = _$rootScope_;
    $timeout = _$timeout_;
    $scope = $rootScope.$new();

    fakePromise = function (){
      return new Promise(function(resolve, reject){
        $timeout(function(){
          resolve('foo');
        }, 100);
      });
    };
  }));

  afterEach(function () {
    $scope.$apply();
  });

  it('should wait for promise to resolve and have a result', function(){
    return fakePromise().should.eventually.equal('foo'); //taken from chai-as-promised readme
  });
}))

自述文件说要做:

return doSomethingAsync().should.eventually.equal("foo");
我得到的错误是:

Chrome 49.0.2623 (Mac OS X 10.11.4) Example should wait for promise to resolve and have a result FAILED
  Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

$timeout
服务已返回承诺。不需要制造

fakePromise = $timeout(function() {return "foo"}, 100);

在本例中,
fakePromise
被设置为一个在100毫秒后解析为“foo”的承诺。

这个怎么样?触发
done()