在同一个控制器中测试Angular modalInstance

时间:2016-06-18 10:02:41

标签: javascript angularjs

我正在使用ui-bootstrap模态窗口,我试图测试一个触发该模态窗口的方法。我的控制器:

app.controller('AddProductController', ['$scope', 'ProductsService', '$uibModal', function ($scope, ProductsService, $uibModal) {
        $scope.product = {};
        $scope.searchCategories = function () {
            ProductsService.getRootCategories().then(function (data) {
                $scope.categories = data.data;
            });
            $scope.modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'categoryContent.html',
                controller: 'AddProductController',
                scope: $scope
            });
            $scope.modalInstance.result.then(function (category) {
                $scope.searchCategory = null;
                $scope.product.category = category;
            }, function () {
            });
        };
        $scope.ok = function(){
            $scope.modalInstance.close($scope.product.category);
        };
        $scope.cancel = function(){
            $scope.modalInstance.dismiss();
    }]);

我的测试:

describe("Products Controller", function () {
beforeEach(function () {
    module('productsController');
});

beforeEach(function () {
    var ProductsService, createController, scope, rootScope,

    module(function ($provide) {
        $provide.value('ProductsService', {
            getRootCategories: function () {
                return {
                    then: function (callback) {
                        return callback({data: {name: 'category1'}});
                    }
                };
            },
        });

        $provide.value('$uibModal', {
            open : function(){
                return {
                    then: function (callback) {
                        return callback({data: {name: 'category1'}});
                    }
                };
            }
        });

        return null;
    });
});
describe('AddProductController', function () {
    beforeEach(function () {
        inject(function ($controller, _$rootScope_, _ProductsService_) {
            rootScope = _$rootScope_;
            scope = _$rootScope_.$new();
            ProductsService = _ProductsService_;
            createController = function () {
                return $controller("AddProductController", {
                    $scope: scope,
                });
            };
        });
    });
    it('calling searchCategories should make $scope.categories to be defined', function () {

        createController();
        expect(scope.categories).not.toBeDefined();  
        scope.searchCategories();
        expect(scope.categories).toBeDefined();
    });
});

});

我的所有测试都通过,除了这个,我得到TypeError:$ scope.modalInstance.result未定义。 有线索吗?

1 个答案:

答案 0 :(得分:1)

您似乎未在模拟模式中定义result。尝试这样的事情:

$provide.value('$uibModal', {
    open: function () {
        return {
            result : {
                then: function (callback) {
                    return callback({ data: { name: 'category1' } });
                }
            }
        };
    }
});