Javascript.confirm() 和 Angularjs Karma e2e 测试 [英] Javascript.confirm() and Angularjs Karma e2e test

查看:17
本文介绍了Javascript.confirm() 和 Angularjs Karma e2e 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Angularjs 应用程序,它在执行某些操作之前使用简单的 javascript 确认.

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

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() 调用,使其不会停止测试的执行?

Now I want to have an end to end test to check the token is being replaced correctly in the view. How can I intercept the javascript.confirm() call so it doesn't stop the execution of the test?

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 函数,但实际调用抱怨它未定义.

So far I've tried to redefine the window.confirm function but then the actual call complains that it is undefined.

我还想在 window.confirm 上设置 Jasmine 间谍,但在以下语法中 spyOn(window, 'confirm'); 它给了我一个错误提示你不能监视 null.

I also wanted to set up a Jasmine spy on window.confirm but in the following syntax spyOn(window, 'confirm'); it gives me an error saying you can not spy on null.

我将如何进行这样的测试?

How would I go about making such test work?

推荐答案

E2E测试

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

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

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:

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

这篇关于Javascript.confirm() 和 Angularjs Karma e2e 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆