Javascript.confirm() and Angularjs Karma e2e test

后端 未结 3 390
暖寄归人
暖寄归人 2021-02-05 15:44

I have an Angularjs application that uses simple javascript confirm before executing some actions.

Controller:

function TokenControll         


        
3条回答
  •  不思量自难忘°
    2021-02-05 16:31

    In unit tests you can mock the $window object like this:

    Your test:

    beforeEach(function() {
        module('myAppName');
    
        inject(function($rootScope, $injector) {
            $controller = $injector.get('$controller');
            $scope = $rootScope.$new();
            var windowMock = { confirm: function(msg) { return true } }
            $controller('UsersCtrl', { $scope: $scope, $window: windowMock });
        });
    });
    

    Your controller:

    myAppName.controller('UsersCtrl', function($scope, $window) {
        $scope.delete = function() {
            var answer = $window.confirm('Delete?');
            if (answer) {
                 // doing something
            }
        }
    });
    

提交回复
热议问题