3

Attempting to mock an http call but getting the error

Error: Unflushed requests: 1 in /Users/..etc/angular-mocks.js

here is my relevant code

describe('Tests for Group Controller', function() {
  var $httpBackend;
  beforeEach(function() {
    module('App');
    return inject(function($injector) {
      var $controller;
      $window = $injector.get('$window');
      $window.ENV = 'http://here.com'
      this.rootScope = $injector.get('$rootScope');
      this.state = {};
      this.stateParams = {};
      this.UserService = $injector.get('UserService');
      this.ProjectService = $q = $injector.get('ProjectService');
      this.ModalService = {};
      $httpBackend = $injector.get('$httpBackend');
      this.GroupService = {};
      $q = $injector.get('$q');
      this.q = $q;
      $controller = $injector.get('$controller');
      this.scope = this.rootScope.$new();
      this.controller = $controller('GroupController', {
        '$scope': this.scope,
        '$state': this.state,
        '$stateParams': this.stateParams,
        "UserService": this.UserService,
        'ProjectService': this.ProjectService,
        'ModalService': this.ModalService,
        'GroupService': this.GroupService,
        '$q': this.q
      });
      // this.scope.$digest();
      $httpBackend.when('GET', 'http://here.com/api/user/2/Group/list?offset=0&max=10&sortField=name&ascending=true').respond({data: 'success'});
    });


  });
  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('should have controller defined', function() {
    expect(this.controller).toBeDefined()
  });

  it('should initGroups', function() {
    $httpBackend.expectGET('http://here.com/api/user/2/Group/list?offset=0&max=10&sortField=name&ascending=true');
    $httpBackend.flush();
  })
});

I'm doing the flush after the expectget but it still says I have an unflushed request. What am i doing wrong?

ceckenrode
  • 4,543
  • 7
  • 28
  • 48

1 Answers1

0

You create GET request before each test, but flush it only in 'Should initGroups' test.