经过大量搜索并尝试在e2e测试中使用工厂后,我决定最终在这里问。
我有一个工厂定义,有一些功能,如下:
regFormApp.factory('myFactory', ['$moment', function($moment) {
return {
title: 'Super Title 2014!',
dates: [
{
date: '2014-10-21',
time: '08:30',
timezone: 'America/Mexico_City'
},
{
date: '2014-10-22',
time: '08:30',
timezone: 'America/Mexico_City'
}
],
prices: {
earlyBird: 1250,
twoMonths: 1375,
last: 1700
},
salesEndDates: function(ticketType) {
var endDate = this.dates[0].date;
switch (ticketType) {
case 'earlyBird':
endDate = '2014-07-19';
break;
case 'twoMonths':
endDate = $moment(endDate)
.subtract('months', 2)
.format('YYYY-MM-DD');
break;
default:
break;
}
return endDate;
},
getSalesPeriod: function(_format) {
var format = _format || 'YYYY-MM-DD';
var now = $moment().format(format);
if (now <= this.salesEndDates('earlyBird')) {
return 'earlyBird';
} else if (now > this.salesEndDates('earlyBird')
&& now <= this.salesEndDates('twoMonths')) {
return 'twoMonths'
}
return 'last';
}
};
}]);
我想在使用量角器的e2e测试中使用getSalesPeriod()
,以测试是否启用了选择。
所以,我的表格看起来像这样:
...
<select id="earlyBirdSelect" class="form-control" ng-model="qty.earlyBird" ng-change="updateTotal()" ng-disabled="disableEarlyBird()">
<option>1</option>
...
</select>
<select id="twoMonthsSelect" class="form-control" ng-model="qty.twoMonths" ng-change="updateTotal()" ng-disabled="disableTwoMonths()">
<option>1</option>
...
</select>
<select id="lastSelect" class="form-control" ng-model="qty.last" ng-change="updateTotal()" ng-disabled="disableLast()">
<option>1</option>
...
</select>
...
用于启用或禁用选择的内容是:
regFormControllers.controller('MainCtrl', ['$scope', '$http', '$filter', 'myFactory',
function($scope, $http, $filter, myFactory) {
...
$scope.disableEarlyBird = function() {
if (myFactory.getSalesPeriod() === 'earlyBird') {
return false;
}
return true;
};
// and analogously for twoMonths and last
...
};
}]);
所以,我想做这样的事情:
describe('form', function() {
beforeEach(function() {
browser.get('app/#/'); // here is the form
});
it('should be enabled only earlyBird select', function() {
var earlyBirdSelect = element(by.css('#earlybirdSelect'));
var twoMonthsSelect = element(by.css('#earlybirdSelect'));
var lastSelect = element(by.css('#earlybirdSelect'));
// here I want to get 'earlyBird' from getSalesPeriod()
var period = myFactory.getSalesPeriod(); // it depends to current date
if (period === 'earlyBird') {
expect(earlyBirdSelect.getAttribute('disabled')).toBeFalsy();
expect(twoMonthsSelect.getAttribute('disabled')).toBeTruthy();
expect(lastSelect.getAttribute('disabled')).toBeTruthy();
}
});
});
我不知道如何测试它。非常感谢任何帮助!
非常感谢你!
答案 0 :(得分:5)
除非您将getSalesPeriod
公开给$scope
,否则没有简单的方法可以做到这一点,在这种情况下您可以lastSelect.evaluate('getSalesPeriod()')
如果您不想更改应用以展示您要调用的功能,则必须通过执行JavaScript进入Angular模块:
browser.executeScript(function() {
return angular.element(document).injector().get('myFactory').getSalesPeriod();
});