Unit-Test eines ModalInstance-Controllers mit Karma / Jasmine

EDIT: Quick & Dirty Lösung am Ende dieses Beitrags

Ich verwende ein modales Fenster von AngularUI-Bootstrap auf die gleiche Weise, wie es auf der Website erklärt wird, mit der Ausnahme, dass ich Dateien aufgeteilt habe. Deshalb habe ich:

CallingController.js:

$scope.delete = function () {
    if ($scope.selected.length > 0) {
        // [...]
        // preparing data
        // [...]
        var modalInstance = $modal.open({
            templateUrl: 'views/modalView.html',
            controller: 'modalCtrl',
            resolve: {
                itemArray: function () {
                    return $scope.selected;
                }
            }
        });
        modalInstance.result.then(function (confirm) {
            if (confirm === true) {
                // [...]
                // treat
                // [...]
            }
        });
    }
};

modalController.js:

myAppControllers.controller('modalCtrl',
    function ($scope, $modalInstance, itemArray) {

        $scope.accept = function () {
            $modalInstance.close(true);
        };

        $scope.reject = function () {
            $modalInstance.close(false);
        };

        $scope.itemArray = itemArray;

    });

und wenn ich diesen Code mit Karma teste (mit demui-bootstrap-tpls.min.js Datei in der Karma-Konfigurationsdatei geladen), erhalte ich den folgenden Fehler:Fehler: [$ injector: unpr] [http://errors.angularjs.org/1.2.15-build.2389+sha.c5f2f58/$ injector / unpr? p0 =% 24modalInstanceProvider% 20% 3C-% 20% 24modalInstance]1 bei Fehler (native)Dies bedeutet, dass Jasmin den Anbieter für $ modalInstance nicht finden kann.

Ich teste noch nicht einmal Sachen auf diesem Controller, aber hier ist meine Jasmin-Testdatei:

testModalController.js:

describe('Controller: modalCtrl', function () {

    beforeEach(module('myApp'));

    var Ctrl;
    var scope;

    // Initialize the controller and a mock scope
    beforeEach(inject(
        function ($controller, $rootScope) {
            scope = $rootScope.$new();

            Ctrl = $controller('modalCtrl', { $scope: scope });
        })
    );

    describe('Initial state', function () {
        it('should instantiate the controller properly', function () {
            expect(Ctrl).not.toBeUndefined();
        });

        it('should initialize its values properly', function () {

        });
    });

});

Haben Sie eine Ahnung von diesem Problem? Es ist nicht das erste "externe" Modul, das ich benutze (und teste), und ich habe das gleiche getan wie für die anderen, außer dass es diesmal nicht funktioniert und ich keine Ahnung habe, warum.

========================================

EDIT: Schnelle & wahrscheinlich schmutzige Lösung:

Okay, basierend auf der Scope-Mocking-Methode in der Controller-Instanziierung von Jasmine habe ich herausgefunden, wie ich mein Problem "lösen" kann, aber es ist wahrscheinlich ziemlich schmutzig. Sie können also gerne einen Kommentar abgeben, wenn Sie einen besseren Weg finden, das zu tun, was ich beabsichtige .

testModalController.js:

describe('Controller: modalCtrl', function () {

    beforeEach(module('myApp'));

    var Ctrl;
    var scope;
    var modalInstance;

    // Initialize the controller and a mock scope
    beforeEach(inject(
        function ($controller, $rootScope, _$modal_) {
            scope = $rootScope.$new();
            modalInstance = _$modal_.open({
                templateUrl: 'views/modalView.html'
            });

            Ctrl = $controller('modalCtrl', {
                $scope: scope,
                $modalInstance: modalInstance,
                itemArray: function () { return ['a', 'b', 'c']; }
            });
        })
    );

    describe('Initial state', function () {
        it('should instantiate the controller properly', function () {
            expect(Ctrl).not.toBeUndefined();
        });

        it('should initialize its values properly', function () {

        });
    });

});

Auf diese Weise sucht Jasmine nicht mehr nach Anbietern, da Sie bereits die Elemente injiziert haben, die diese Anbieter benötigen sollen. Es funktioniert, aber ich glaube, es könnte besser gemacht werden ...

Antworten auf die Frage(3)

Ihre Antwort auf die Frage