25

編集:この投稿の最後にあるクイック&ダーティソリューション

ファイルを分割したことを除いて、ウェブサイトで説明されているのと同じ方法で、AngularUI-Bootstrap のモーダル ウィンドウを使用しています。したがって、私は持っています:

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;

    });

このコードをカルマ (カルマ構成ファイルに読み込まれたui-bootstrap-tpls.min.jsファイル) でテストすると、次のエラーが表示されます: エラー: [$injector:unpr] [ http://errors. angularjs.org/1.2.15-build.2389+sha.c5f2f58/ $injector/unpr?p0=%24modalInstanceProvider%20%3C-%20%24modalInstance] 1 at Error (native)、ジャスミンが管理していないことを意味します$modalInstance のプロバイダーを見つけます。

私はまだこのコントローラーで何かをテストしていませんが、ここに私のジャスミンテストファイルがあります:

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 () {

        });
    });

});

この問題について手がかりはありますか? これは、私が使用 (およびテスト) した最初の「外部」モジュールではなく、他のモジュールと同じことを行いましたが、今回は機能せず、理由がわかりません。

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

EDIT:クイック&おそらく汚い解決策:

さて、ジャスミンのコントローラーのインスタンス化におけるスコープのモック方法に基づいて、問題を「解決」する方法を見つけましたが、おそらくかなり汚いので、私が意図したことを行うためのより良い方法を見つけたら、遠慮なくコメントしてください.

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 () {

        });
    });

});

このようにすると、Jasmine はプロバイダーを検索しなくなります。これらのプロバイダーを必要としていると思われるアイテムを既に挿入しているためです。それは機能しますが、より良い方法で実行できると思います...

4

3 に答える 3

7

それ以外の:

modalInstance = {                    // Create a mock object using spies
  close: jasmine.createSpy('modalInstance.close'),
  dismiss: jasmine.createSpy('modalInstance.dismiss'),
  result: {
    then: jasmine.createSpy('modalInstance.result.then')
  }
};

これは次のように記述できます。

modalInstance = jasmine.createSpyObj('modalInstance', ['close', 'dismiss', 'result.then']);

また、$modalInstance はなく、現在は $uibModalInstance であるため、上記のすべての「modalInstance」を「uibModalInstance」に置き換える必要があります。

于 2016-04-11T23:13:44.960 に答える