Javascript angular.js断言错误抛出异步

Javascript angular.js断言错误抛出异步,javascript,angularjs,asynchronous,assert,throw,Javascript,Angularjs,Asynchronous,Assert,Throw,我正在使用karma和sinon chai测试angular.js应用程序。我需要测试这样的代码: var sync = function() { async.then(function() { // success }, function() { throw Error('foo'); }); } sync是我想要测试的功能async是一个返回$q承诺的函数,我想测试它是否失败并抛出错误 我理想的测试套装: describe('unit', function()

我正在使用
karma
sinon chai
测试
angular.js
应用程序。我需要测试这样的代码:

var sync = function() {
  async.then(function() {
    // success
  }, function() {
    throw Error('foo');
  });
}
sync
是我想要测试的功能
async
是一个返回
$q
承诺的函数,我想测试它是否失败并抛出错误

我理想的测试套装:

describe('unit', function() {
  it('should throw an error', function() {
    expect(function() {
      return sync();
    }).to.throw(Error, 'foo');
  })
});
但是,它在
afterEach()
失败,因为错误是在
$digest
上抛出的

我怎样才能完成这个测试


多谢各位

嗨,你成功地让它工作了吗?