2

The app works perfectly fine, but when I run Karma to unit test the controller...

This is the error:

Error: [$injector:unpr] Unknown provider: NgMapProvider <- NgMap
http://errors.angularjs.org/1.4.10/$injector/unpr?p0=NgMapProvider%20%3C-%20NgMap

this is my test:

describe('dashBoardEmployeeCtrl', function(){
  var ctrl, scope, $httpBackend
  var data = [{longitude: 1, latitude: 2, description: "cool office", name: "new your office"}]

  beforeEach(function(){
    module('dashBoardApp.employeeLocations');
    inject(function($controller, $rootScope, _$httpBackend_, NgMap){
      $httpBackend = _$httpBackend_;
      $httpBackend.expectGET('http://localhost:3000/something.json').respond(data)
      scope = $rootScope.$new();
      ctrl = $controller('dashBoardEmployeeCtrl', {$scope:scope})
    });
  });

  it('has a attribute called $scope.employeeLocations that returns an object from api', function(){
    $httpBackend.flush()
    expect(scope.Locations).toEqual([{longitude: 1, latitude: 2, description: "cool office", name: "new your office"}])
  })

});

This is my Karma.config.js

files : [
  'http://maps.google.com/maps/api/js',
  'app/bower_components/angular/angular.js',
  'app/bower_components/ngmap/build/scripts/*.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/components/**/*.js',
  'app/dashBoard/*.js',
  'app/services/*.js',
  'app/bower_components/ngmap/build/scripts/ng-map.min.js',
  'app/dashBoard/*.html',
  'unit-tests/*.js',
],
Hedu
  • 61
  • 4
  • NgMap is an external service, thus angular cannot inject it during testing. You need to give a mocked version of it that will do just enough so that your test is working. When you have that mock you can then pass it the same way you inject the $scope. – sam May 16 '16 at 10:01
  • thanks sam, may you please give me an example? I am a very junior developer, coding for few month only :_D – Hedu May 16 '16 at 10:08
  • well then can you give some piece of your controller, the bit that uses NgMap – sam May 16 '16 at 10:10
  • its fine, passing now! thanks man =) – Hedu May 16 '16 at 10:11

0 Answers0