3
我正在与browserify
捆绑角度服务。我使用jasmine
撰写对这一服务,该服务等被定义测试:测试角度服务也是一个承诺
angular
.module('Client', [])
.factory('client', ['url', 'otherService', '$http', '$log', client])
function client (url, otherService, $http, $log) {
$log.debug('Creating for url %s', url)
var apiRootPromise = $http.get(url).then(function (apiRoot) {
$log.debug('Got api root %j', apiRoot)
return otherService.stuff(apiRoot.data)
})
return Object.assign(apiRootPromise, otherService)
}
下面的测试套件:
describe('test client', function() {
beforeEach(function() {
angular.mock.module('Client')
angular.mock.module(function ($provide) {
$provide.value('url', 'http://localhost:8080/')
})
})
it('should connect at startup', angular.mock.inject(function (client, $rootScope, $httpBackend) {
$rootScope.$apply()
$httpBackend.flush()
expect(client).toBeDefined()
}))
})
抛出TypeError: undefined is not a constructor
上(evaluating Object.assign(apiRootPromise, otherService)')
。我不确定这里发生了什么,但我最好的猜测是Angular没有正确注入依赖服务或者没有返回$http
的承诺。