大佬教程收集整理的这篇文章主要介绍了Javascript.confirm()和Angularjs Karma e2e测试,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
控制器:
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’);它给我一个错误,说你不能间谍空.
我如何进行这样的测试工作?
请咨询本项目:
https://github.com/katranci/Angular-E2E-Window-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'; } }; }@H_927_2@modalDialog服务
yourApp.factory('modalDialog',['$window',function($window) { return { confirm: function(messagE) { return $window.confirm(messagE); } } }]);@H_927_2@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测试全部内容,希望文章能够帮你解决Javascript.confirm()和Angularjs Karma e2e测试所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。