且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

对于控制器angualr因缘茉莉单元测试

更新时间:2023-02-25 16:32:42

这个问题来自于 myApp.factories ,我没有看到这个声明的模块中的任何地方因果报应文件守望者。控制器前行,把你的 myApp.factories 模块的路径,应该很好地工作。

I am adding a userController module to my SPA project with Jasmine on Karma. I got an error saying "Error: [$injector:modulerr] Failed to instantiate module myApp.controllers due to: Error: [$injector:nomod] Module 'myApp.controllers' is not available!". The app works fine. It just the unit test doesn't see the controller module for some reason. Thanks for your help.

/// <reference path="~/scripts/_references.js" />
/// <reference path="~/scripts/controllers/userController.js" />

'use strict';

describe('Controllers: userCtrl', function() {
      var scope, ctrl;

      beforeEach(function() {
        module('myApp.controllers');
      });

      beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        ctrl = $controller('userCtrl', {
          $scope: scope
        });
      }));

      it("Should be false", function() {
        expect(false).toBeFalsy();
      });
    }

The mainController.js contains only two lines with ['myApp.factories'] as dependency in the module declaration like this:

'use strict';

angular.module('myApp.controllers', ['myApp.factories']);

The actual controllers are in their own .js files, and have angular.module('myApp.controllers') without [ ] in the declaration. The userController.js is like this:

'use strict';

angular.module('myApp.controllers')
  .controller('userCtrl', ['$scope', '$location', '$window', 'version',
    function ($scope, $location, $window, version)
    {
      $scope.$root.title = 'AngularJS SPA | User';
      $scope.appVersion = version;
      $scope.title = 'User';
      $scope.content = "my contents goes here";
    }]);

In karma.conf.js, I have the following in the files: property.

files: [
          'bower_components/angular/angular.js',
          'bower_components/angular-ui-router/release/angular-ui-router.js',
          'bower_components/angular-mocks/angular-mocks.js',
          'bower_components/angular-resource/angular-resource.js',
          'bower_components/jquery/dist/jquery.js',
          'myApp/scripts/controllers/*.js',
          'myApp/scripts/tests/*.spec.js',
          'myApp/scripts/tests/**/*.spec.js'
        ],

The issue comes from myApp.factories, I don't see this declared module anywhere in the karma files watcher. Before the controllers line, put the path of your myApp.factories module and should work fine.