我一直收到错误:[$ injector:unpr]未知提供者:modalInstanceProvider< - modalInstance 我觉得我离这个测试非常接近,但我一直陷入困境。如果有人有任何提示我会很感激。我花了几天时间没有运气。
谢谢
Ctrl Test
describe('DeleteModalController', function () {
//make module avalible to tests
beforeEach(module('pb.accounts.controllers'));
beforeEach(module('ui.router'));
beforeEach(module('ui.bootstrap'));
var $controller;
var mockGlobal = { activeOrganizationId: 0 };
var mockStateParams = { orgId: 1, entityId: null };
var mockForm = {};
var mockState = {};
var mockAccountSrv = {
account: {
entityId: 2,
page: 19,
length: 200
},
accounts: {
entityId: 2,
page: 19,
length: 200
}
};
// instantiating controller
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));
describe("Account service delete() function", function () {
var controller, scope;
// sets scope of controller before each test
beforeEach(inject(function ($rootScope, _$modal_, _$modalInstance_) {
scope = $rootScope.$new();
controller = $controller('DeleteModalController',
{
$modal: _$modal_,
modalInstance: _$modalInstance_,
$scope: scope,
$stateParams: mockStateParams,
$state: mockState,
global: mockGlobal,
accountService: mockAccountSrv
});
}));
it("make sure service promise resolves", function () {
scope.delete(mockAccountSrv.account);
spyOn(modalInstance, "close");
scope.$digest();
expect(modalInstance.close).toHaveBeenCalled();
});
});
});
DeleteCtrl
angular.module('pb.accounts.controllers')
.controller('DeleteModalController', ['global', '$scope', 'accountService', '$modal', '$modalInstance', 'account', function (global, $scope, accountService, $modal, $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');
};
}]);
帐户Ctrl
angular.module('pb.accounts.controllers')
.controller('AccountsController', ['$scope', '$q', '$stateParams', '$state', '$timeout', '$modal', 'global', 'accountService', function ($scope, $q, $stateParams, $state, $timeout, $modal, global, accountService) {
var init = function () {
global.activeOrganizationId = $stateParams.orgId || 0;
$scope.global = global;
$scope.partialViews = {
form: "/app/accounts/views/_form.html"
};
$scope.currentEntityId = $stateParams.entityId || 0;
};
init();
$scope.getAll = function (page, length) {
accountService.getAccounts(global.activeOrganizationId, page, length).then(function (data) {
$scope.accounts = data;
});
};
$scope.get = function (entityId) {
accountService.getAccount(global.activeOrganizationId, entityId).then(function (data) {
$scope.account = data;
});
};
$scope.add = function (form, account) {
form.submitIfValid(
function () {
return accountService.createAccount(global.activeOrganizationId, account);
}).then(function (data) {
$state.go('accounts.campaigns.list', { orgId: global.activeOrganizationId, accountId: data.entityId });
});
};
$scope.edit = function (form, account) {
form.submitIfValid(
function () {
return accountService.updateAccount(global.activeOrganizationId, account)
}).then(function (data) {
$state.go('accounts.campaigns.list', { orgId: global.activeOrganizationId, accountId: account.entityId });
});
};
$scope.confirmDelete = function (account) {
var modalInstance = $modal.open({
templateUrl: '/app/accounts/views/_delete.html',
controller: 'DeleteModalController',
resolve: {
account: function () {
return account;
}
}
});
modalInstance.result.then(function (asset) {
$scope.getAll(1, 100);
}, function () {
console.log('Modal dismissed at: ' + new Date());
});
};
}]);
答案 0 :(得分:4)
$modalInstance
是作为$modal
调用的一部分创建的,它不是全局依赖项,因此您不能这样做:
beforeEach(inject(function ($rootScope, _$modal_, _$modalInstance_) {
您需要做的是创建虚拟$modalInstance
或具有与$modalInstance
相同功能的间谍。
这样的事情:
modalInstance = { // Create a mock object using spies
close: jasmine.createSpy('modalInstance.close'),
dismiss: jasmine.createSpy('modalInstance.dismiss'),
result: {
then: jasmine.createSpy('modalInstance.result.then')
}
};
取消这个答案Unit testing a modalInstance controller with Karma / Jasmine
答案 1 :(得分:0)
我认为您应该使用$modalInstance
来初始化您的控制器,而不是modalInstance
controller = $controller('DeleteModalController', {
$modal: _$modal_,
$modalInstance: _$modalInstance_,
$scope: scope,
$stateParams: mockStateParams,
$state: mockState,
global: mockGlobal,
accountService: mockAccountSrv
});