服务引发未知的提供程序错误-Angularjs单元测试

时间:2019-01-23 18:38:18

标签: angularjs unit-testing jasmine karma-jasmine karma-runner

我正在尝试修复很久以前设置的一些损坏的单元测试。由于某些原因,当我们运行测试时,它们均会因每次服务注入都会出现的“未知提供程序错误”而全部失败。我进行了很多搜索,但看不到测试的任何明显问题。如果测试没有问题,这可能是配置问题吗?我一直在尝试加载文件的顺序,但这似乎无关紧要。

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

1 个答案:

答案 0 :(得分:1)

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

确保CatalogControllerloggerServicecatalogServicefeedbackService属于photonControllersPreSession模块,或者它们的模块包含在{{1 }}。

例如,如果photonControllersPreSession是其他模块的一部分,则假设loggerService,请确保已像这样包含mySuperModule

mySuperModule

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

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