mrt*_*mrt 12 jasmine angularjs karma-runner angularjs-e2e
我有一个Angularjs应用程序,在执行某些操作之前使用简单的javascript确认.
function TokenController($scope) {
$scope.token = 'sampleToken';
$scope.newToken = function() {
if (confirm("Are you sure you want to change the token?") == true) {
$scope.token = 'modifiedToken';
}
};
}
Run Code Online (Sandbox Code Playgroud)
<div id="tokenDiv">
Token:{{token}} <button ng-click="newToken()">New Token</button>
</div>
Run Code Online (Sandbox Code Playgroud)
现在我想要进行端到端测试以检查在视图中是否正确替换了令牌.如何拦截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);
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试重新定义window.confirm函数,但实际的调用抱怨它是未定义的.
我也想设置一个Jasmine间谍,window.confirm但是在下面的语法中,spyOn(window, 'confirm');它给了我一个错误,说你不能窥探null.
我将如何进行这样的测试工作?
And*_*i V 27
另一个选择是直接创建一个间谍并自动返回true:
//Jasmine 2.0
spyOn(window, 'confirm').and.callFake(function () {
return true;
});
//Jasmine 1.3
spyOn(window, 'confirm').andCallFake(function () {
return true;
});
Run Code Online (Sandbox Code Playgroud)
kat*_*nci 13
请参考这个项目: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';
}
};
}
Run Code Online (Sandbox Code Playgroud)
yourApp.factory('modalDialog', ['$window', function($window) {
return {
confirm: function(message) {
return $window.confirm(message);
}
}
}]);
Run Code Online (Sandbox Code Playgroud)
function modalDialogMock() {
this.confirmResult;
this.confirm = function() {
return this.confirmResult;
}
this.confirmTrue = function() {
this.confirmResult = true;
}
this.confirmFalse = function() {
this.confirmResult = false;
}
}
Run Code Online (Sandbox Code Playgroud)
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');
});
Run Code Online (Sandbox Code Playgroud)
小智 7
在单元测试中,您可以像这样模拟$ window对象:
你的考试:
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 });
});
});
Run Code Online (Sandbox Code Playgroud)
你的控制器:
myAppName.controller('UsersCtrl', function($scope, $window) {
$scope.delete = function() {
var answer = $window.confirm('Delete?');
if (answer) {
// doing something
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13080 次 |
| 最近记录: |