【发布时间】:2015-11-21 11:15:06
【问题描述】:
我按照“Hands on agular”课程的说明 (https://code.tutsplus.com/courses/hands-on-angular),编写了以下控制器测试:
'use strict';
describe('Controller: EventsController', function () {
// load the controller's module
beforeEach(module('ekApp'));
var EventsController,
scope, http, response;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
http = $httpBackend;
response = [{ key: '1' }];
http.whenGET('/api/events').respond(response);
scope = $rootScope.$new();
EventsController = $controller('EventsController', {
$scope: scope
// place here mocked dependencies
});
}));
afterEach(function(){
http.verifyNoOutstandingExpectation();
http.verifyNoOutstandingRequest();
});
it('should request to api', function () {
http.expectGET('/api/events');
http.flush();
});
});
当我运行此测试时,我收到“没有待处理的刷新请求”错误...
这是我的事件控制器:
'use strict';
angular.module('ekApp')
.controller('EventsController', function($scope, Event, Category){
$scope.categories = [{name: 'All'}];
$scope.serverCategories = Category.query(function(){
$scope.categories = $scope.categories.concat($scope.serverCategories);
});
console.log($scope.categories);
$scope.events = Event.query();
console.log($scope.events);
$scope.filterBy = {
search: '',
category: $scope.categories[0],
startDate: new Date(2015,4,1),
endDate: new Date(2016,1,14)
};
});
还有我的事件服务,它返回资源:
'use strict';
angular
.module('ekApp')
.factory('Event', function($resource){
return $resource('/api/events/:id', { id: '@id' });
});
【问题讨论】:
标签: angularjs unit-testing karma-jasmine