【问题标题】:Unit test $mdDialog angular material单元测试 $mdDialog 角度材料
【发布时间】:2017-08-11 08:12:29
【问题描述】:

我在函数内部调用了一个 $mdDialog。我想对 $mdDialog ok 进行单元测试并取消案例。

以下是我的控制器代码 (app.controller.js)。

 (function () {
    'use strict';

    app.controller('AppCtrl', AppCtrl);

    AppCtrl.$inject = ['$scope', '$mdDialog'];

    function AppCtrl($scope, $mdDialog) {

        $scope.saveEntry = function (ev) {
            var confirm = $mdDialog.prompt()
                .title('Save Entry')
                .textContent('If you want, you can add a description to explain what you changed.')
                .placeholder('Version Description')
                .ariaLabel('Version Description')
                .initialValue('')
                .targetEvent(ev)
                .ok('Save')
                .cancel('Cancel');

            $mdDialog.show(confirm).then(function (result) {
                $scope.status = true;
            }, function () {
                $scope.status = false;
            });
        };
    }

})();

以下是规范代码(app.controller.spec.js):

describe('Unit test AppController: mdDialog', function () {
    var $controller, $mdDialog;
    beforeEach(function () {
        module('App');
        inject(function (_$controller_, _$mdDialog_) {
            $controller = _$controller_;
            $mdDialog = _$mdDialog_;
        });
    });

    it(': Opened', function () {
        var $scope = {};
        var controller = $controller('AppCtrl', { $scope: $scope });

        var $mdDialogOpened = false;
        $mdDialog.show = jasmine.createSpy().and.callFake(function () {
            $mdDialogOpened = true;
        });

        $scope.saveEntry();
        $scope.$digest();

        expect($mdDialog.show).toHaveBeenCalled;
        expect($mdDialogOpened).toBe.true;
    });
});

当我运行上述代码时,我收到以下错误: TypeError: Cannot read property 'then' of undefined

我提到了这个 GitHub 问题 https://github.com/angular/material/issues/1482。但我的问题没有得到解决方案

提前致谢

【问题讨论】:

    标签: angularjs unit-testing angular-material karma-jasmine angular-mock


    【解决方案1】:

    问题是您正在注入一个版本的 $mdDialog,并尝试在另一个版本上进行测试。
    你可以试试这样的:

    describe('Unit test AppController: mdDialog', function () {
        var ctrl, mdDialog, scope;
    
        beforeEach(function () {
            module('App');
            inject(function ($rootScope, $controller, $mdDialog) {
            scope = $rootScope.$new();
            mdDialog = $mdDialog; //keep the reference, for later testing.
    
            spyOn(mdDialog, 'show');
            mdDialog.show.and.callFake(function () {
            return {
                then: function (callBack) {
                            callBack(true); //return the value to be assigned.
                        }
            }
            });     
    
            ctrl = $controller('AppCtrl',{$scope:scope, $mdDialog:mdDialog}); //Inject the dependency
    
            });
        });
    
        it(': Opened', function () {
            scope.saveEntry(); //exercise the method.
            scope.$digest();
    
            expect(mdDialog.show).toHaveBeenCalled();
            expect(scope.status).toBe(true);
        });
    });
    

    非常相似的东西应该可以工作。
    希望对您有所帮助。

    【讨论】:

    • 感谢您的回复。我认为您错过了 $rootScope.new() 中的 $,请更新您的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2018-06-17
    • 2020-09-28
    相关资源
    最近更新 更多