gpt4 book ai didi

javascript - Angular- Jasmine : Unknown provider: $modalProvider <- $modal

转载 作者:行者123 更新时间:2023-11-27 22:53:59 28 4
gpt4 key购买 nike

Jasmine 和 Angular 测试的新手。我正在代码中测试 Controller ,现在我只想检查我的 Controller 是否已定义。我收到错误 Unknown provider: $modalProvider <- $modal因为它取决于$modal服务,因为我在 Controller 中使用它。这是我的 Controller 代码:

(function (){

angular.module('app.uploadedReleases')
.controller('UploadedReleasesController', UploadedReleasesController)
.controller('ModalController', ModalController)
.controller('StatusModalController', StatusModalController);

var ACTION = {
CANCEL: 0,
SAVE: 1,
DELETE: 2,
SUBMIT: 3,
REFRESH: 4
};

UploadedReleasesController.$inject = ['$log', '$scope', '$filter', '$modal', 'ReleaseService', 'TrackService', 'APP_CONFIG', 'DeliveriesService'];
function UploadedReleasesController ($log, $scope, $filter, $modal, releaseService, trackService, APP_CONFIG, deliveriesService){

// and in the same controller I define the Modal Controller as follows
.....
....
function ModalController($modalInstance, formAlbum, isRelease, formTrack){
var vm = this;


vm.formTrackData = formTrack;

function saveReleaseConfirm(){
$modalInstance.close({action: ACTION.SAVE});
}

function removeTrack(){
$modalInstance.close({action: ACTION.DELETE});
}

function removeAlbum(){
$modalInstance.close({action: ACTION.DELETE});
}

function cancel(){
$modalInstance.close({action: ACTION.CANCEL});
}

function createDeliveryConfirm(){
$modalInstance.close({action: ACTION.SUBMIT});
}
}

现在在我的 Jasmine 测试中,我将其定义如下:

describe('app module', function() {
var vm, scope, releaseService, trackService, deliveriesService;

beforeEach(module('app.uploadedReleases')); // Main module name

beforeEach(module('app.config')); // for the APP_CONFIG dependency
beforeEach(module('auth')); // for the $auth dependency
beforeEach(function() {

});


beforeEach(inject(function($controller, $log, $rootScope, $filter, $modal, APP_CONFIG) {

scope = $rootScope.$new();
vm = $controller('UploadedReleasesController', {'APP_CONFIG':APP_CONFIG, '$log':$log, '$scope':scope, '$filter':$filter, '$modal':$modal,
'ReleaseService':releaseService, 'TrackService':trackService, 'DeliveriesService':deliveriesService});

}));


describe("Tests UploadedReleases controller to be defined", function() {

it("should be created successfully", function() {
expect(vm).toBeDefined();
});
});

知道我应该如何注入(inject) $modal 来解决这个问题吗?谢谢

最佳答案

以下是如何在规范中注入(inject) $modal 的方法。

describe('app module', function() {
var vm, scope, releaseService, trackService, deliveriesService, $modal;

beforeEach(module('app.uploadedReleases')); // Main module name

beforeEach(module('app.config')); // for the APP_CONFIG dependency
beforeEach(module('auth')); // for the $auth dependency
beforeEach(function() {

});


beforeEach(inject(function($controller, $log, $rootScope, $filter, $modal, APP_CONFIG, _$modal_) {

scope = $rootScope.$new();
$modal = _$modal_
vm = $controller('UploadedReleasesController', {
'APP_CONFIG':APP_CONFIG,
'$log':$log,
'$scope':scope,
'$filter':$filter,
'$modal':_$modal_,
'ReleaseService':releaseService,
'TrackService':trackService,
'DeliveriesService':deliveriesService
});

}));


describe("Tests UploadedReleases controller to be defined", function() {

it("should be created successfully", function() {
expect(vm).toBeDefined();
});
});

关于javascript - Angular- Jasmine : Unknown provider: $modalProvider <- $modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37778513/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com