错误:未刷新请求Karma

时间:2016-08-03 15:28:44

标签: angularjs jasmine karma-runner httpbackend

尝试模拟http调用但收到错误

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

这是我的相关代码

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

我在期待之后正在做同花,但它仍然说我有一个未满足的请求。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您在每次测试之前创建GET请求,但仅在'应该initGroups'测试