samedi 21 novembre 2015

Jasmine: no pending request to flush

I was following the instructions of the course 'Hands on agular' (http://ift.tt/1OXQpFv), wrote the following controller test:

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

When i run this test, i get 'no pending request to flush' error...

Here is my 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)
    };
});

and my Event service, which returns resource:

'use strict';

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

Aucun commentaire:

Enregistrer un commentaire