Javascript 测试未运行服务模拟承诺(.then)
我试图创建一个模拟我的Javascript 测试未运行服务模拟承诺(.then),javascript,angularjs,unit-testing,testing,jasmine,Javascript,Angularjs,Unit Testing,Testing,Jasmine,我试图创建一个模拟我的BoardService。但是,运行测试时,.then功能没有在控制器中运行,但在运行实际应用程序时,它工作正常 以下是测试: beforeEach(inject(function($rootScope, $controller, $q) { scope = $rootScope.$new(); BoardController = $controller('BoardController', { $scope: scope,
BoardService
。但是,运行测试时,.then
功能没有在控制器中运行,但在运行实际应用程序时,它工作正常
以下是测试:
beforeEach(inject(function($rootScope, $controller, $q) {
scope = $rootScope.$new();
BoardController = $controller('BoardController', {
$scope: scope,
board: {
id: 1,
tasks: []
},
BoardService: {
addTask: function(data) {
var defer = $q.defer();
defer.resolve(1);
return defer.promise;
}
}
});
}));
it("should add a new task", function() {
scope.addTask(category, 'this is a new task');
expect(scope.board.tasks.length).toBe(1);
});
和控制器功能:
$scope.addTask = function(category, task) {
BoardService.addTask({name: task, category: category.id, boardId: $scope.board.id}).then(function(task_id) {
// Never reaches here
$scope.board.tasks.push({id : task_id, name : task, category : category.id, board_id : $scope.board.id});
});
}
为什么它从未到达。then
函数中的注释?解决承诺后,您需要触发一个摘要周期,以便angular能够获取它
scope.addTask(category, 'this is a new task');
scope.$digest();
expect(scope.board.tasks.length).toBe(1);
在解决承诺之后,您需要触发一个摘要周期,以便angular能够理解它
scope.addTask(category, 'this is a new task');
scope.$digest();
expect(scope.board.tasks.length).toBe(1);
在解决承诺之后,您需要触发一个摘要周期,以便angular能够理解它
scope.addTask(category, 'this is a new task');
scope.$digest();
expect(scope.board.tasks.length).toBe(1);
在解决承诺之后,您需要触发一个摘要周期,以便angular能够理解它
scope.addTask(category, 'this is a new task');
scope.$digest();
expect(scope.board.tasks.length).toBe(1);
正如我们在IRC上讨论的那样:您需要调用scope。$digest用于启动承诺。正如我们在IRC上讨论的那样:您需要调用scope。$digest用于启动承诺。正如我们在IRC上讨论的那样:您需要调用scope。$digest用于启动承诺。正如我们在IRC上讨论的那样:您需要调用scope。$digest用于启动承诺