我正在尝试对外部承诺的解决/拒绝进行单元测试。
问题是这些承诺在ngMock
注入模块时永远不会完成 - 这是强制性的,因为我正在触发 HTTP 请求和超时。
以angular-pouchdb 为例,它是我项目的依赖项。从 v2.0.0 开始,由于使用了 PouchDB 承诺,因此在注入 ngMock 时测试不再完成。
示例代码,改编自angular-pouchdb 测试:
var scope;
beforeEach(function() {
var $injector = angular.injector(['ng', 'ngMock', 'pouchdb']);
var pouchDB = $injector.get('pouchDB');
scope = $injector.get('$rootScope');
db = pouchDB('db');
});
it('should wrap destroy', function(done) {
db.destroy()
.then(shouldBeOK)
.catch(shouldNotBeCalled)
.finally(done);
// No matter what's used ($apply, $applySync, $timeout.flush, etc) -- the tests will never finish.
scope.$apply();
});
上述测试的日志如下:
C:\Users\Gustavo\Projetos\main\angular-pouchdb (master)
λ karma start
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 8 0.0.0)]: Connected on socket s1A7jIzmtcOxkGCtzYke with id 42899233
PhantomJS 1.9.8 (Windows 8 0.0.0) Angular-aware PouchDB public API should wrap destroy FAILED
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
所以,我的问题归结为:单元测试 Angular 是否缺少一些东西?还是 ngMock 的内部错误?