0

我在我的 AngularJS 应用程序中测试 $http 时遇到了一些问题。我试图创建以下对我来说失败的示例:

控制器JS:

var testController = ['$scope', '$http', function($scope, $http) {
    $http.get('/test').success(function(data) {
      //TODO  
    });
}];

控制器测试JS:

describe("Test Controller", function () {
    var scope, httpMock;
    beforeEach(module('MailChimpApp'));

    beforeEach(inject(function ($injector, $rootScope) {
        httpMock = $injector.get('$httpBackend');
        scope = $rootScope.$new();
    }));

    afterEach(function () {
        httpMock.verifyNoOutstandingExpectation();
        httpMock.verifyNoOutstandingRequest();
    });

    it('testing post', function () {
        httpMock.expectGet('/test').respond([{ test: 'true' }]);

        var controller = scope.$new('testController');

        httpMock.flush();
    });
});

当我使用 Jasmin 在 Karma 中运行测试时,出现以下错误:

 TypeError: Object function $httpBackend(method, url, data, callback, headers) {
      var xhr = new MockXhr(),
          expectation = expectations[0],
          wasExpected = false;

      function prettyPrint(data) {
        return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
            ? data
            : angular.toJson(data);
      }

      if (expectation && expectation.match(method, url)) {
        if (!expectation.matchData(data))
          throw Error('Expected ' + expectation + ' with different data\n' +
              'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT:      ' + data);

        if (!expectation.matchHeaders(headers))
          throw Error('Expected ' + expectation + ' with different headers\n' +
              'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT:      ' +
              prettyPrint(headers));

        expectations.shift();

        if (expectation.response) {
          responses.push(function() {
            var response = expectation.response(method, url, data, headers);
            xhr.$$respHeaders = response[2];
            callback(response[0], response[1], xhr.getAllResponseHeaders());
          });
          return;
        }
        wasExpected = true;
      }

      var i = -1, definition;
      while ((definition = definitions[++i])) {
        if (definition.match(method, url, data, headers || {})) {
          if (definition.response) {
            // if $browser specified, we do auto flush all requests
            ($browser ? $browser.defer : responsesPush)(function() {
              var response = definition.response(method, url, data, headers);
              xhr.$$respHeaders = response[2];
              callback(response[0], response[1], xhr.getAllResponseHeaders());
            });
          } else if (definition.passThrough) {
            $delegate(method, url, data, callback, headers);
          } else throw Error('No response defined !');
          return;
        }
      }
      throw wasExpected ?
          Error('No response defined !') :
          Error('Unexpected request: ' + method + ' ' + url + '\n' +
                (expectation ? 'Expected ' + expectation : 'No more request expected'));
    } has no method 'expectGet'
      at null.<anonymous>

当我尝试执行 httpMock.expectGET 时发生错误。我记得在我的 karma 配置中包含 angular-mocks.js,如下所示:

JASMINE,
  JASMINE_ADAPTER,
  'Scripts/jquery-1.10.2.min.js',
  'Scripts/angular.js',
  'Scripts/angular-mocks.js',
  'Scripts/angular-resource.js',
  'Scripts/App.js',
  'Scripts/Controllers/*.js',
  'Unittests/JavaScript/Controllers/*.js'

使用 $httpBackend 模拟我的 $http-requests 时我可能会缺少什么吗?

4

2 回答 2

2

答案是将 beforeEach 更改为以下内容:

 beforeEach(inject(function ($injector, $rootScope, $httpBackend) {
        httpMock = $httpBackend;
        scope = $rootScope.$new();
          ctrl = $controller(testController, { $scope: scope });
    }));
于 2013-08-02T12:43:27.690 回答
-1

确保包含模块ngMock

在这里我只看到 beforeEach(module('MailChimpApp'));

于 2013-07-29T11:31:06.633 回答