我有以下工厂
angular.module('reports.detailed.service', ['ngResource'])
.factory('ReportsDetailed', ['$resource', function($resource) {
var Reports = $resource('../api/detailedReports', {
}, {
query: {method: 'GET', isArray: true},
count: {method: 'GET', params: {method: 'count'}},
sendEmail: {method: 'POST', isArray:true}
});
return Reports;
}]);
控制器及其功能:
.controller('DetailedReportIndexCtrl', ['$scope', 'ReportsDetailed', function($scope, ReportsDetailed) {
loadReport();
function loadReport() {
$scope.report = ReportsDetailed.query(/* parameters here */).$then(function(result){
var days = [];
var currentDate = moment($scope.timeFrame.from);
while(currentDate < $scope.timeFrame.to) {
var day = {
date: moment(currentDate),
times: getTimesForDay(currentDate)
};
days.push(day);
currentDate.add('days', 1);
}
return days;
function getTimesForDay(day) {
var times = [];
angular.forEach(result.data, function(item, idx) {
item.timeFrom = moment(item.timeFrom);
item.timeTo = moment(item.timeTo);
if(currentDate.isSame(item.date)) {
times.push(item);
}
});
return times;
}
}).then(function(data){ // it fails here when calling this controller from unit test
$scope.report = data;
});
}
}])
在模板中我像这样使用控制器:
<tbody ng-repeat="day in report">
</tbody>
我得到了day变量,我可以在模板中使用它。我不明白的是,为什么 我的单元测试失败了:
describe('detailed reports', function() {
var $scope, $controller, ReportsDetailed, Client, Project, Task, AccountUser, security, UserProject;
beforeEach(module('app'));
beforeEach(inject(function($injector) {
$scope = $injector.get('$rootScope');
$controller = $injector.get('$controller');
$dialog = $injector.get('$dialog');
ReportsDetailed = $injector.get('ReportsDetailed');
Client = $injector.get('Client');
Project = $injector.get('Project');
Task = $injector.get('Task');
AccountUser = $injector.get('AccountUser');
security = $injector.get('security');
UserProject = $injector.get('UserProject');
}));
describe('DetailedReportCtrl', function() {
var params, ctrl, dialogCtrl;
/* details not important for this example */
indexCtrl = $controller('DetailedReportIndexCtrl', {$scope:$scope}); // it fails here
});
使用以下消息调用'DetailedReportIndexCtrl'时测试失败:
[31mChrome 32.0.1700(Windows)详细报告DetailedReportCtrl应该通过虚假测试FAILED [39m TypeError:Object [object Array]没有方法'then' 在loadReport(D:/Projects/Eva/Eva.App/src/app/reports/detailed/reports.detailed.js:190:6)
当我使用angular 1.0.8版本时测试成功,但现在在1.2.12测试失败。有趣的是,当我删除
时,该测试是成功的.then(function(data){
$scope.report = data;
});
来自控制器,但后来我没有在模板中得到任何结果。
我几天都在苦苦挣扎,我真的可以帮忙。
谢谢, 贾尼