2

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).

1 Answer 1

2

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 });
Sign up to request clarification or add additional context in comments.

6 Comments

now it's giving error Cannot read property '$scope' of undefined :(
Also I tried with writing addUserController = $controller('AddUserController', {$scope: scope, $http: $http, $state: $state, UserFactory: userFactory}) with no luck.
Argument 'AddUserController' is not a function, got undefined
Have ypu referred controller script on the page.. also refer all the dependent angular file as well
You have to refer AddUserController.js + other angular component files which you are going test and file which has app defined
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.