服务抛出未知提供程序错误 - Angularjs 单元测试

Services throwing unknown provider error - Angularjs Unit Testing

我正在尝试修复很久以前设置的一些损坏的单元测试。出于某种原因,当我们 运行 测试时,它们都失败了,引用了我们为每个服务注入获得的 "unknown provider error"。我做了很多搜索,但我看不到任何明显的测试问题。如果测试没有问题,这可能是配置问题吗?我试过文件的加载顺序,但似乎无关紧要。

"use strict";

describe("Catalogs controller", function() {

  beforeEach(angular.mock.module("photonControllersPreSession"));



  var $rootScope;

  var $scope;
  var createController;
  var $window;
  var $location;
  var loggerService;
  var catalogService;
  var feedbackService;


  beforeEach(
    inject( function(
      $controller,
      _$rootScope_,
      _$window_,
      _$location_,
      _loggerService_,
      _catalogService_,
      _feedbackService_
    ) {
      $rootScope = _$rootScope_;
      $window = _$window_;
      $location = _$location_;
      loggerService = _loggerService_;
      catalogService = _catalogService_;
      feedbackService = _feedbackService_;
      $scope = $rootScope.$new();

      spyOn(loggerService, "info");

      createController = function() {
        return $controller("CatalogController", {
          $scope: $scope,
          $location: $location,
          $window: $window,
          loggerService: _loggerService_,
          catalogService: _catalogService_,
          feedbackService: _feedbackService_
        });
      };
    })
  );


  it("Should init", function() {
    var catalogController = null;
    catalogController = createController();
    console.log("test: " + createController);


    // Just want to see if the controller is created.
    expect(catalogController).not.toBe(null);
  });
});

AngularJS 确实要求在开始测试之前加载所有模块。您只有一个模块 photonControllersPreSession 包含在这个特定的测试套件中。

确保 CatalogControllerloggerServicecatalogServicefeedbackService 属于 photonControllersPreSession 模块,或者它们的模块包含在 photonControllersPreSession 还有。

例如,如果 loggerService 是某个其他模块的一部分,比方说 mySuperModule,请确保 mySuperModule 已像这样包含在内

angular.module('photonControllersPreSession', [
  'mySuperModule'  
]);

否则您必须在每次测试前手动包含所有模块

beforeEach(() => {
  module('mySuperModule');
  module('photonControllersPreSession');
});