【问题标题】:unit testing $('#modalId').modal('show'); undefined单元测试 $('#modalId').modal('show');不明确的
【发布时间】:2017-05-05 03:17:03
【问题描述】:

我是单元测试和 angularjs 的新手。

当我对我的模态进行单元测试时,无论它们是否显示,都会返回一个错误:

> TypeError: undefined is not a constructor (evaluating
> '$('#modalId').modal('show')')

如何对我的模态进行单元测试?

【问题讨论】:

    标签: javascript angularjs unit-testing jasmine chutzpah


    【解决方案1】:

    这取决于您使用的测试框架。在jasmine 的情况下,您可以监视模态。假设你有这个控制器:

    'use strict';
    
    angular.module('angularUiModalApp')
        .controller('MainCtrl', function($scope, $modal, $log) {
            $scope.items = ['item1', 'item2', 'item3'];
    
            $scope.open = function() {
    
                $scope.modalInstance = $modal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    resolve: {
                        items: function() {
                            return $scope.items;
                        }
                    }
                });
    
                $scope.modalInstance.result.then(function(selectedItem) {
                    $scope.selected = selectedItem;
                }, function() {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };
        })
        .controller('ModalInstanceCtrl', function($scope, $modalInstance, items) {
            $scope.items = items;
            $scope.selected = {
                item: $scope.items[0]
            };
    
            $scope.ok = function() {
                $modalInstance.close($scope.selected.item);
            };
    
            $scope.cancel = function() {
                $modalInstance.dismiss('cancel');
            };
        });
    

    那么测试是这样的:

    'use strict';
    
    describe('Controller: MainCtrl', function() {
    
        // load the controller's module
        beforeEach(module('angularUiModalApp'));
    
        var MainCtrl,
            scope;
    
        var fakeModal = {
            result: {
                then: function(confirmCallback, cancelCallback) {
                    //Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
                    this.confirmCallBack = confirmCallback;
                    this.cancelCallback = cancelCallback;
                }
            },
            close: function( item ) {
                //The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
                this.result.confirmCallBack( item );
            },
            dismiss: function( type ) {
                //The user clicked cancel on the modal dialog, call the stored cancel callback
                this.result.cancelCallback( type );
            }
        };
    
        beforeEach(inject(function($modal) {
            spyOn($modal, 'open').andReturn(fakeModal);
        }));
    
    
        // Initialize the controller and a mock scope
        beforeEach(inject(function($controller, $rootScope, _$modal_) {
            scope = $rootScope.$new();
            MainCtrl = $controller('MainCtrl', {
                $scope: scope,
                $modal: _$modal_
            });
        }));
    
        it('should show success when modal login returns success response', function() {
            expect(scope.items).toEqual(['item1', 'item2', 'item3']);
    
            // Mock out the modal closing, resolving with a selected item, say 1
            scope.open(); // Open the modal
            scope.modalInstance.close('item1');
            expect(scope.selected).toEqual('item1'); 
            // No dice (scope.selected) is not defined according to Jasmine.
        });
    });
    

    您需要返回 $modal.open 通常返回的 mock,而不是 $modal 的模拟,它不包括 open 函数在 fakeModal 模拟中布局。假模态必须有一个 result 对象,其中包含一个 then 函数来存储回调(在单击 OK 或 Cancel 按钮时调用)。它还需要一个close 函数(模拟一个确定按钮单击模态)和一个dismiss 函数(模拟一个取消按钮单击模态)。 close和dismiss函数在调用时调用必要的回调函数。

    【讨论】:

    • 我这样称呼我的模态:$('#modalId').modal('show');
    猜你喜欢
    • 2015-01-07
    • 2017-11-22
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多