javascript - 在同一 Controller 内测试 Angular modalInstance

标签 javascript angularjs

我正在使用 ui-bootstrap 模态窗口,并且我正在尝试测试一种触发该模态窗口的方法。我的 Controller :

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 is undefined。 有什么线索吗?

最佳答案

您似乎没有在模拟模式中定义结果。尝试这样的事情:

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

关于javascript - 在同一 Controller 内测试 Angular modalInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895722/

相关文章:

javascript - n 秒后或单击按钮时自动关闭 div

javascript - 谷歌图表 API 的自定义标签和位置

javascript - 为什么我的 Google App 脚本期望函数位于文件中而不是脚本中?

javascript - 从 Angular Controller 访问输入字段会导致未定义

angularjs - 如何将 Okta SignIn 小部件和 SAML SSO 与 Angularjs SPA 集成?

javascript - 使用jquery淡出后隐藏div,而不是之前

javascript - 将 _this 传递给 ajax 调用

javascript - 在 ng-click 上更新 AngularJS 模型,DOM 没有响应

angularjs - 是否有必要将 $scope 注入(inject)到 angularjs 中的 Controller 中?

javascript - AngularJS Rest GET 请求不起作用