Angularjs 测试modalInstance.result.then
我终于开始学习如何使用我编写的旧angularjs应用程序进行测试。我的控制器中有一些modals,我一辈子都不知道如何确保运行“modalInstance.result.then”中的代码并对其进行测试 我搜索了谷歌等网站,找到了一些测试模态的例子,但到目前为止,他们似乎都在测试模态控制器本身 我如何得到承诺(modalInstance.result.then)来解决?我已尝试运行$modal.close(),但失败的原因是出现了错误。我试过用jasmine间谍软件等多种方式嘲笑modalInstance和$modal。我对测试的无知阻碍了我。任何帮助都将被告知 这是我的controller.js:Angularjs 测试modalInstance.result.then,angularjs,unit-testing,jasmine,bootstrap-modal,karma-jasmine,Angularjs,Unit Testing,Jasmine,Bootstrap Modal,Karma Jasmine,我终于开始学习如何使用我编写的旧angularjs应用程序进行测试。我的控制器中有一些modals,我一辈子都不知道如何确保运行“modalInstance.result.then”中的代码并对其进行测试 我搜索了谷歌等网站,找到了一些测试模态的例子,但到目前为止,他们似乎都在测试模态控制器本身 我如何得到承诺(modalInstance.result.then)来解决?我已尝试运行$modal.close(),但失败的原因是出现了错误。我试过用jasmine间谍软件等多种方式嘲笑modalIn
(function() {
var comment = angular.module('APP.comment', ['APP.user']);
var commentController = function($scope, $modal) {
var self = this;
self.addComment = function(newComment) {
var modalInstance = $modal.open({
templateUrl: 'views/commentModal.html',
backdrop: 'static',
windowClass: 'modal',
controller: 'commentModalController',
controllerAs: 'commentCtrl',
resolve: {
newComment: function() {
return newComment;
}
}
});
modalInstance.result.then(function(data) {
// How do I test that the function or branches here
// were run?
if (data.length === 2) {
// do this thing
} else {
// do this other thing
}
});
};
};
commentController.$inject = ['$scope', '$modal'];
comment.controller('commentController', commentController);
}());
以下是我迄今为止的测试:
describe('Unit: commentController', function() {
var $rootScope,
$scope,
$controller,
$modal;
beforeEach(module('APP.comment'));
beforeEach(inject(function(_$rootScope_, _$controller_, _$modal_) {
$modal = _$modal_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
$controller = _$controller_('commentController as commentCtrl', {
$scope: $scope,
$modal: $modal,
});
}));
it('should have controller defined', function() {
expect($scope.qaCtrl).toBeDefined();
});
it('should have method defined', function() {
expect($scope.qaCtrl.addComment).toBeDefined();
});
describe('$scope.commentCtrl.addComment', function() {
it('should open modal', function() {
$scope.commentCtrl.addComment();
});
});
});
我这里有一个plnkr:it('should open modal', inject(function($q) {
var fakeResult = {
result: $q.when([])
};
spyOn($modal, 'open').and.returnValue(fakeResult);
$scope.commentCtrl.addComment();
$scope.$apply();
// now check that the right thing has been done, given the empty array returned
}));