Jasmine:没有待处理的冲洗请求

时间:2015-11-21 11:15:07

标签: angularjs unit-testing karma-jasmine

我遵循了课程的指示' 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();
  });
});

当我运行此测试时,我得到了没有待处理的待清除请求#39;错误...

这是我的EventsController:

'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)
    };
});

和我的Event服务,它返回资源:

'use strict';

angular
    .module('ekApp')
    .factory('Event', function($resource){
        return $resource('/api/events/:id', { id: '@id' });
    });

1 个答案:

答案 0 :(得分:0)

只需向上滚动终端,您将看到实际错误:

Unknown provider: $resourceProvider <- $resource <- Event

您需要使用angular-resource作为依赖项,并将其作为依赖模块添加到您的应用程序中。

angular.module('ekApp',['ngResource'])

另外,不要忘记在karma-conf.js中添加angular-resource.js

  'app/bower_components/angular-resource/angular-resource.js',