1

everywhere in application my company created we used this example of how to create a controller :

app.myFunnyController = function($scope.....){}

but i see that everywhere in test people are using this way of creating controllers:

app.controller('myFunnyController', function ($scope) {
}

And i can see that when i am creating my test and using app.myFunnyController declaration:

'use strict';
describe('publicCtrl', function(){
 beforeEach(module('app')); 
    it("should be true", inject(function($controller){
       var scope = {},
        ctrl = $controller('myFunnyController', {$scope : scope});
       expect(scope.data).toBe("test2");

    }));
})

I getting an error of myFunnyController is not a function. If i using the second type of declaration, everything works fine. Why does this happend?

An other problem is that i am getting error: scope is not defined. I am new to Karma and Unit testing for front end, what am i doing wrong?

1
  • Please further specify your second issue in a separate question. I can't see any obvious reason why scope should not be defined, so we may need more info. Commented Apr 5, 2014 at 2:37

1 Answer 1

3

From my understanding, the second syntax (app.controller(...)) registers the controller function on the module. The first syntax just adds the controller function as an attribute of the module. app.controller does a bit more magic under the hood so that when you call $controller('myFunnyController, ...) in your test, the module knows about the controller and can run it. This is the recommended way to define controllers according to the angular controllers guide.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.