Javascript.confirm() and Angularjs Karma e2e test

后端 未结 3 389
暖寄归人
暖寄归人 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:10

    Another option would be to directly create a spy and automatically return true:

    //Jasmine 2.0
    spyOn(window, 'confirm').and.callFake(function () {
         return true;
    });
    
    //Jasmine 1.3
    spyOn(window, 'confirm').andCallFake(function () {
         return true;
    });
    
    0 讨论(0)
  • 2021-02-05 16:11

    E2E Testing

    Please consult to this project: https://github.com/katranci/Angular-E2E-Window-Dialog-Commands

    Unit Testing

    If you create a service for the dialog boxes then you can mock that service in your unit test in order to make your code testable:

    Controller

    function TokenController($scope, modalDialog) {
      $scope.token = 'sampleToken';
    
      $scope.newToken = function() {
        if (modalDialog.confirm("Are you sure you want to change the token?") == true) {
          $scope.token = 'modifiedToken';
        }
      };
    }
    

    modalDialog service

    yourApp.factory('modalDialog', ['$window', function($window) {
        return {
            confirm: function(message) {
                return $window.confirm(message);
            }
        }
    }]);
    

    modalDialogMock

    function modalDialogMock() {
        this.confirmResult;
    
        this.confirm = function() {
            return this.confirmResult;
        }
    
        this.confirmTrue = function() {
            this.confirmResult = true;
        }
    
        this.confirmFalse = function() {
            this.confirmResult = false;
        }
    }
    

    Test

    var scope;
    var modalDialog;
    
    beforeEach(module('yourApp'));
    
    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        modalDialog = new modalDialogMock();
        var ctrl = $controller('TokenController', {$scope: scope, modalDialog: modalDialog});
    }));
    
    it('should be able to generate new token', function () {
       modalDialog.confirmTrue();
    
       scope.newToken();
       expect(scope.token).toBe('modifiedToken');
    });
    
    0 讨论(0)
  • 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
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题