【发布时间】:2014-05-15 20:46:25
【问题描述】:
在我的控制器中,我有:
$scope.index = function() {
CompanyService.initialized.then(function() {
var company_id = CompanyService.getCompany()._id;
LocationService.list(company_id, $routeParams.location_parent_id).then(function(response) {
if(response.data.status === 'ok') {
$scope.locations = response.data.locations;
$scope.current_location = response.data.location || null;
}
});
});
}
所以应该得到LocationService列表,测试如下:
it('should get locations', function() {
$httpBackend.when('GET', '/api/v1/location/list').respond({status: 'ok', locations: ['loc1', 'loc2']})
$scope.index();
expect($scope.locations.length).toEqual(2);
然而,这永远不会发生,因为CompanyService 有一个在单元测试中永远不会得到解决的承诺。如何模拟 CompanyService 的返回承诺或绕过它?
【问题讨论】:
标签: javascript angularjs unit-testing promise