代码之家  ›  专栏  ›  技术社区  ›  Snake Eyes

数组不是javascript单元测试中的构造函数

  •  1
  • Snake Eyes  · 技术社区  · 6 年前

    我有一个用于创建对话框的工厂:

    module myModule {
        export class myDialog {
            constructor(private $uibModal: ng.ui.bootstrap.IModalService) {}
            showDialog() {
                var options: ng.ui.bootstrap.IModalSettings = {
                    templateUrl: '/dialog.html',
                    size: "lg",
                    controller: ['$scope', '$uibModalInstance', function($scope: any, $uibModalInstance: ng.ui.bootstrap.IModalServiceInstance) {
                        $scope.cancel = () => {
                            $uibModalInstance.close({
                                doAction: 'close'
                            });
                        }
                    }],
                    controllerAs: '$ctrl'
                };
                return this.$uibModal.open(options).result;
            }
    
            static factory(): any {
                const dialog = ($uibModal: ng.ui.bootstrap.IModalService) => new myDialog($uibModal);
                dialog.$inject = ['$uibModal'];
                return dialog;
            }
        };
        angular.module('myModule').factory('myDialog', myDialog.factory());
    }
    

    如你所见,为了 controller

    我使用bardjs创建了一个测试:

    describe('My dialog service', function() {
        beforeEach(function() {
            module('myModule', function($provide) {
                $provide.factory('$uibModalInstance', function() {
                    return {
                        close: function(result) {
                            return result;
                        }
                    };
                });
            });
            module('myModule');
            bard.inject('$uibModal', '$uibModalInstance', '$http', '$httpBackend', '$q', '$rootScope', 'myDialog');
            bard.mockService($uibModal, {
                open: function(options) {
                    return {
                        result: options
                    };
                }
            });
            spyOn($uibModal, 'open').and.callThrough();
            spyOn($uibModalInstance, 'close').and.callThrough();
        });
    
        it('expect it to be defined', function() {
            expect(myDialog).toBeDefined();
        });
    });
    

    TypeError:数组不是构造函数(正在计算) 'options.controller(作用域,$uibModalInstance'))

    为什么?如何解决?

    1 回复  |  直到 6 年前
        1
  •  0
  •   barbsan Cibi    6 年前

    在外部声明控制器 options 对象和用途 $inject 注入依赖项。

    showDialog() {
        const ctrl = function($scope: any, $uibModalInstance: ng.ui.bootstrap.IModalServiceInstance) {
                    $scope.cancel = () => {
                        $uibModalInstance.close({
                            doAction: 'close'
                        });
                    }
                };
        ctrl.$inject = ['$scope', '$uibModalInstance'];
    
        var options: ng.ui.bootstrap.IModalSettings = {
                templateUrl: '/dialog.html',
                size: "lg",
                controller: ctrl,
                controllerAs: '$ctrl'
        };
        return this.$uibModal.open(options).result;
    }