Angular-Jasmine:未知提供者:$ modalProvider< - $ modal

时间:2016-06-12 19:56:47

标签: javascript angularjs modal-dialog jasmine

Jasmine和Angular测试新手。我正在我的代码中测试一个控制器,现在我只想检查我的控制器是否已定义,开头。我收到错误Unknown provider: $modalProvider <- $modal,因为它依赖于$modal服务,因为我在控制器中使用它。这是我的控制器代码:

(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();
        });
    });

任何想法我应该如何注入$ modal来摆脱这个问题?感谢

1 个答案:

答案 0 :(得分:1)

以下是您在规范中注入$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();
    });
});