【发布时间】:2015-03-09 17:53:29
【问题描述】:
几天来,我一直试图找到一种方法来测试这个控制器部件,但一直卡住。现在我得到一个 ReferenceError: Can't find variable: $modal 但我已经注入了它,所以我不确定它为什么不起作用。我也知道我正在编写的这个测试并没有真正测试任何重要的东西,所以如果您对前进有任何建议,请告诉我。并感谢任何在整个控制器中帮助我编写代码的人
代码:
$scope.confirmDelete = function (account) {
var modalInstance = $modal.open({
templateUrl: '/app/accounts/views/_delete.html',
controller: function (global, $scope, $modalInstance, account) {
$scope.account = account;
$scope.delete = function (account) {
global.setFormSubmitInProgress(true);
accountService.deleteAccount(global.activeOrganizationId, account.entityId).then(function () {
global.setFormSubmitInProgress(false);
$modalInstance.close();
},
function (errorData) {
global.setFormSubmitInProgress(false);
});
};
$scope.cancel = function () {
global.setFormSubmitInProgress(false);
$modalInstance.dismiss('cancel');
};
},
resolve: {
account: function () {
return account;
}
}
});
测试:
describe("confirmDelete() function", function () {
var controller, scope;
// sets scope of controller before each test
beforeEach(inject(function ($rootScope, _$modal_) {
scope = $rootScope.$new();
controller = $controller('AccountsController',
{
$scope: scope,
$stateParams: mockStateParams,
$state: mockState,
// below: in order to call the $modal have it be defined and send on the mock modal?
$modal: _$modal_,
//modalInstance: mockModalInstance,
global: mockGlobal,
accountService: mockAccountSrv
});
}));
beforeEach(inject(function ($modal, $q) {
spyOn($modal, 'open').and.returnValue({
result: $q.defer().promise
});
}));
it("make sure modal promise resolves", function () {
scope.confirmDelete(mockAccountSrv.account);
expect($modal.open).toHaveBeenCalled();
});
});
【问题讨论】:
-
我有,那个和这个stackoverflow.com/questions/22905581/… 帮助了我很多,但我似乎无法将拼图拼凑在一起
标签: angularjs unit-testing jasmine