Javascript.confirm()和Angularjs噶E2E测试(Javascript.co

2019-09-02 04:25发布

我有一个使用简单的JavaScript执行确认的一些行动之前Angularjs应用。

控制器:

function TokenController($scope) {
  $scope.token = 'sampleToken';

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

视图:

<div id="tokenDiv">
  Token:{{token}} <button ng-click="newToken()">New Token</button>
</div>

现在,我想有一个端到端的测试,以检查令牌被在视图中正确地更换。 我怎样才能拦截javascript.confirm()调用,这样它不会停止测试的执行?

测试:

it('should be able to generate new token', function () {
   var oldValues = element('#tokenDiv').text();
   element('button[ng-click="newToken()"]').click(); // Here the javascript confirm box pops up.
   expect(element('#tokenDiv').text()).not.toBe(oldValues);
});

到目前为止,我已经试过重新定义window.confirm功能,但后来的实际通话抱怨说,它是不确定的。

我也想建立一个茉莉花刺探window.confirm但在语法如下spyOn(window, 'confirm'); 它给了我一个错误说你不能窥视null

我怎么会去这样做测试工作?

Answer 1:

端到端测试

请咨询到这个项目: https://github.com/katranci/Angular-E2E-W​​indow-Dialog-Commands

单元测试

如果您创建对话框服务,那么你可以模拟该服务在您的单元测试,以使你的代码可测试:

调节器

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服务

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;
    }
}

测试

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');
});


Answer 2:

另一种选择是直接创建一个间谍,并自动返回true

//Jasmine 2.0
spyOn(window, 'confirm').and.callFake(function () {
     return true;
});

//Jasmine 1.3
spyOn(window, 'confirm').andCallFake(function () {
     return true;
});


Answer 3:

在单元测试中,你可以嘲笑这样的$窗口对象:

您的测试:

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 });
    });
});

控制器:

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


文章来源: Javascript.confirm() and Angularjs Karma e2e test