M. Bhamre
M. Bhamre

Reputation: 65

Unknown provider in test case for angular with es6 module

I have to write karma-jasmine test case for controller

(->

class AddUserController
  @$inject = ['$scope', '$http', '$state', 'UserFactory']

  constructor: (@$scope, @$http, @$state, UserFactory) ->
    @user = new UserFactory()



angular
.module('app', [])
.controller('AddUserController', AddUserController)
)()

but when I inject AddUserController in test case it gives me unknown provider:

describe('add_user_controller', function() {
  var addUserController, $httpBackend;

  beforeEach(module("app"));

  beforeEach(
    inject( function($injector, $rootScope) {
      addUserController = $injector.get('AddUserController')
    })
  );
  it('should have initialize values', function() {
    expect(addUserController.user).toBeDefined();
  })
});

Can any one guess, whats going wrong here.

Here is karma.conf.js code

module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    files: [
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js',
      '*.coffee',
      'test/*.coffee'
    ],
    preprocessors: {
      '*.coffee': ['coffee']
    },
    plugin: [
      'karma-coffee-preprocessor',
      'karma-jasmine',
      'karma-chrome-launcher',
    ],
    autoWatch: true,
    browsers: ['Chrome']
  });
};

my addUserController.coffee and karma.conf.js is on same root(level).

Upvotes: 2

Views: 99

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You should get instance of controller by passing controller name to $controller service. Like below

scope = $rootScope.$new(true);
//inject `$controller` before use it.
addUserController = $controller('AddUserController', { $scope: scope });

Upvotes: 2

Related Questions