jeudi 4 février 2016

Argument 'xxController' is not a function in unit test

I'm new to Angular and unit testing. I'm trying to write a unit test for my Controller. I followed this documentation. Now I am trying to do the same thing with my code but there are too many things that I don't understand. I keep getting "Error: [ng:areq] Argument 'graphController' is not a function, got undefined".

This is my controller in graphController.js

angular.module('graphController',[])
  .config(['$logProvider', function($logProvider) {
    $logProvider.debugEnabled(false);
  }])
  .controller('graphController', function($log, graphFactory, $scope) {

  ...

  });

I have dependency on graphFactory:

angular.module('graphModule')
.factory('graphFactory', function($http){

  var graph = {};
  var apiRoot = 'http://localhost:3000/graph/';

  graph.getAllNodes = function(agency_key){
    var url =  apiRoot + 'getAllNodes/' + agency_key;
    return $http({
      method:'GET',
      url: url
    })
  };
...

return graph;
})
.factory('_', ['$window', function($window) {
  return $window._;
}]);

In ControllerSpec.js, I have this:

describe('Test Controllers', function(){
  beforeEach(module('app'));
  module('graphController');

  var $controller;
  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  describe('graphController', function(){
    it('Trying to make it work', function(){
      var $graphFactory = {};
      var $scope = {};
      var controller = $controller('graphController', {$scope:$scope, $graphFactory:$graphFactory});
  });
  });
});

  1. If I comment out the last line in the test file (var controller = ...), it doesn't crash when i run gulp test task. Why am I getting a Bad Argument error in this line?

  2. What does {$scope:$scope} means in this line? I saw it in the documentation I followed and try to repeat it. Obviously it's not working with when I have additional parameters (such as graphFactory or $log).

  3. I understand I need to use the inject function for the dependencies. I also should mock the dependencies. How am i going to mock a dependency which also has another dependency? I tried to use jasmine.createSpyObj('graphFactory', [all method names I have in this file])but I don't understand how to use it. I am completely lost where and how to use $provide, jasmine spies, $rootScope..

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire