【发布时间】:2016-02-27 12:02:22
【问题描述】:
我正在与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()
}))
})
在(evaluating Object.assign(apiRootPromise, otherService)') 上抛出一个TypeError: undefined is not a constructor。我不确定这里发生了什么,但我最好的猜测是 Angular 没有正确注入依赖服务或没有返回 $http 承诺。
【问题讨论】:
标签: angularjs node.js jasmine browserify