$ httpBackend.verifyNoOutstandingExpectation()导致"没有定义响应!"错误

时间:2015-08-07 17:28:41

标签: angularjs karma-runner

My understanding的{p> $httpBackend.verifyNoOutstandingExpectation()就是它基本上说'#34;确保您的测试$httpBackend.expect()所有的请求都是由您的代码完成的。如果他们中的任何一个没有,我会抛出异常。"

1)这似乎是多余的。是不是相应的$httpBackend.expect()会失败,从而提醒你呢?

2)当我使用No response defined !时,我收到$httpBackend.verifyNoOutstandingExpectation()错误。当我删除$httpBackend.verifyNoOutstandingExpectation()时,错误消失了,我的测试通过了。为什么会这样?

enter image description here

admin.controller.spec.js:14是指$httpBackend.verifyNoOutstandingExpectation()来电。

admin.controller.spec.js

describe('AdminController', function() {
  var AdminController, scope, $httpBackend;

  beforeEach(module('mean-starter'));
  beforeEach(inject(function($controller, $rootScope, _$httpBackend_) {
    $httpBackend = _$httpBackend_;
    scope = $rootScope.$new();
    AdminController = $controller('AdminController', {
      $scope: scope
    });
  }));

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

  it('gets users', function() {
    $httpBackend.expectGET('/users');
  });
});

admin.controller.js

angular
  .module('mean-starter')
  .controller('AdminController', AdminController);

function AdminController(User, Auth, $state) {
  var vm = this;
  User
    .list()
    .success(function(data) {
      vm.users = data;
    })
    .error(function() {
      console.log('Problem getting users.');
    });

  vm.delete = function(id) {
    User
      .delete(id)
      .success(function(data) {
        if (Auth.getCurrentUser()._id === id) Auth.logout(); // deleting yourself
        else $state.reload();
      })
      .error(function() {
        console.log('Problem deleting user.');
      });
  };
}

1 个答案:

答案 0 :(得分:2)

错误消息清楚地说明了问题 - 您需要先定义预期的响应,然后才能验证期望是否已满足。

更改

$httpBackend.expectGET('/users');

$httpBackend.expectGET('/users').respond([{
  //Response object or array
}]);