I am following the angularjs tutorial here : https://docs.angularjs.org/tutorial/step_02
My original controller:
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope) {
$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.'}
];
});
original test:
'use strict';
/* jasmine specs for controllers go here */
describe('PhoneListCtrl', function () {
beforeEach(module('phonecatApp'));
it("should create 'phones' model with 3 phones", inject(function ($controller) {
var scope = {},
ctrl = $controller('PhoneListCtrl', {$scope: scope});
expect(scope.phones.length).toBe(3);
}));
});
New controller (using the 'this' way of things)
Angular: Should I use this or $scope
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function () {
this.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.'
}
];
});
How should the test change to make things work?
Currently I see the following error
Chrome 41.0.2272 (Mac OS X 10.10.2) PhoneListCtrl should create 'phones' model with 3 phones FAILED
TypeError: Cannot read property 'length' of undefined
at null.<anonymous> (/Users/somghosh/Programming/angular-phonecat/test/unit/controllersSpec.js:13:32)
at Object.invoke (/Users/somghosh/Programming/angular-phonecat/app/bower_components/angular/angular.js:4185:17)
at workFn (/Users/somghosh/Programming/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2364:20)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/somghosh/Programming/angular-phonecat/app/bower_components/angular-mocks/angular-mocks.js:2335:25)
at null.<anonymous> (/Users/somghosh/Programming/angular-phonecat/test/unit/controllersSpec.js:9:58)
at /Users/somghosh/Programming/angular-phonecat/test/unit/controllersSpec.js:5:5
Chrome 41.0.2272 (Mac OS X 10.10.2): Executed 1 of 1 (1 FAILED) ERROR (0.009 secs / 0.009 secs)