Pregunta

I have a module called MainCtrl in file MainCtrl.js

angular.module("MainCtrl",['ui.bootstrap']);

I'm trying to accomplish a Jasmine test (using Karma) to validate controller. Ideally, my app would simply have a module MainCtrl and controllers, then let me to separate all contorllers in different files.

I have controllers in other fil like loginCtrl in ht file loginCtrl.js

angular.module('MainCtrl').controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

            //code
        }
]);

In my unit test i try this:

'use strict';

describe("Controllers", function() {

  beforeEach(module('App'));
  beforeEach(module('MainCtrl'));
  beforeEach(module('services'));

  describe("Unit Test LoginCtrl", function() {

    var ctrl, Factory, scope, $compile , $httpBackend;

    beforeEach(inject(function(_$compile_, _$httpBackend_, $rootScope, $controller,$injector) {
      $httpBackend = _$httpBackend_;
      $compile = _$compile_;

      Factory = $injector.get('Factory');

      scope = $rootScope.$new();
      ctrl = $controller("LoginCtrl", {$scope: scope});

    }));

    it('should have a ReportIndividual controller', function() {
      expect(ctrl).toBeDefined();
    });
  });
});

app.js

var app = angular.module('App', [
    'MainCtrl',
    'services',
]);

My karma.conf.js

files: [
    'jasmine.js',
    'adapter.js',
    'angular.js',
    'angular-route.js',
    'angular-mocks.js',
    'jquery.min.js',
    'app.js',
    {pattern: 'scripts/*.js', watched: true, included: true, served: true},
    {pattern: 'tests/unit/*Spec.js', watched: true, included: true, served: true}
],

When i start test i get this message in browser console :

"minErr/<@http://localhost:9876/base/angular.js:78
loadModules/<@http://localhost:9876/base/angular.js:3703
forEach@http://localhost:9876/base/angular.js:322
loadModules@http://localhost:9876/base/angular.js:3668
createInjector@http://localhost:9876/base/angular.js:3608
workFn@http://localhost:9876/base//angular-mocks.js:2139

When i add [] to the declaration module the test work but not the app :( :

 angular.module('MainCtrl',[]).controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

            //code
        }
]);

Thinks for help :)

¿Fue útil?

Solución

angular.module('MainCtrl',[]).controller("LoginCtrl", ['$scope','Factory',
    function ($scope, Factory) {

        //code
    }
]);

The above definition is correct. What is the error you are getting after adding '[]'?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top