I am create my controllers with the module().controller() syntax.
angular.module('App', []);
angular.module('App').controller('PhoneListCtrl', ['$scope', function ($scope) {
'use strict';
$scope.phones = [
{
"name": "Nexus S",
"snippet": "Fast just got faster with Nexus S."
},
{
"name": "Motorola XOOM™ with Wi-Fi",
"snippet": "The Next, Next Generation tablet."
},
{
"name": "MOTOROLA XOOM™",
"snippet": "The Next, Next Generation tablet."
}
];
}]);
That works great. But now I want to test it with jasmine, and my test respond with "ReferenceError: AppController is not defined".
My test:
/* jasmine specs for controllers go here */
describe('PhoneCat controllers', function () {
describe('PhoneListCtrl', function () {
it('should create "phones" model with 3 phones', function () {
var scope = {},
ctrl = new PhoneListCtrl(scope);
expect(scope.phones.length).toBe(3);
});
});
});
If I change the controller to a classic function, the test works fine.
What do I missing?
In your test you should not create controller instances "by hand" but let AngularJS create instances using the $controller service. This is necessary since AngularJS DI system needs to have a chance to inject dependencies.
Your test should roughly look like:
beforeEach(module('App'));
it('should create "phones" model with 3 phones', inject(function ($controller, $rootScope) {
var ctrl = $controller('PhoneListCtrl', {$scope: $rootScope});
expect($rootScope.phones.length).toBe(3);
}));