【发布时间】:2016-04-12 04:04:48
【问题描述】:
有没有办法在量角器测试中使用.controller() 方法访问指令的控制器?在单元测试中我可以做到
var el = angular.element(...);
$compile(el)(scope);
var component = el.controller('nameOfDirective');
但我似乎无法弄清楚如何在量角器测试中做到这一点。
这是我要测试的代码...
//registration.route.js
(function () {
'use strict';
angular.module('app.account.registration').run(appRun);
appRun.$inject = ['routerHelper'];
/* @ngInject */
function appRun(routerHelper) {
routerHelper.configureStates(getStates());
}
function getStates() {
return [
{
state: 'registration',
config: {
url: '/registration',
template: '<registration layout="column" flex/>',
title: 'registration'
}
}
];
}
})();
//registration.component.js
(function () {
'use strict';
angular.module('app.account.registration').component('registration', {
templateUrl: 'app/account/registration/registration.html',
controller: Controller
});
/* @ngInject */
function Controller($state, UserService, SweetAlert) {
var self = this;
this.$onInit = function () {
this.state = 'unverified';
this.processing = false;
};
this.register = function (formData) {
this.processing = true;
UserService.register(formData.email).then(function (response) {
self.state = 'verify';
self.email = response.data.email;
//response.data.token will only be returned on localhost for security reasons
self.token = response.data.token;
self.processing = false;
}).catch(function (error) {
if (error.status === 422) {
SweetAlert.error('Error!', formData.email + ' already exists. Please use a unique email.');
}
self.processing = false;
});
};
}
})();
【问题讨论】:
-
你想从控制器中得到什么?
-
我正在端到端测试注册页面,该页面向用户发送带有“确认电子邮件”链接的电子邮件。该链接有一个我用来完成注册的令牌。我需要令牌来“伪造”点击电子邮件。有没有更好的测试方法?编辑:令牌在对
Service.register(userEmail)的响应中返回,这是从我的指令中调用的。 -
所以您是说您希望
Service.register(userEmail)返回某种假令牌而不是其正常行为? -
好吧,我想实际访问服务器,这意味着
Service.register将返回一个真正的令牌。除非我不应该使用真正的 REST api?但它看起来并不是真正的端到端测试......我想这是值得商榷的。 -
不,它可以/应该使用真正的 REST api。
标签: angularjs angularjs-directive protractor