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

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

问题描述

我有一个 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 但在下面的语法 spyOn(窗口,'确认'); 它给了我一个错误说你不能窥探

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?

推荐答案

请咨询这个项目:
<一href=\"https://github.com/katranci/Angular-E2E-Window-Dialog-Commands\">https://github.com/katranci/Angular-E2E-Window-Dialog-Commands

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

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噶端到端测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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