Reputation: 67
I'm working on a small project and I currently ran into an issue using this over $scope. I'm not sure which is best but I'm not stuck because karma won't find 'this' in my controller when I write up a jasmine test. Here is my jasmine test code:
'use strict';
describe('PhoneListCtrl', function() {
beforeEach(module('phoneCtrlModule'));
it('should create "phones" model with 3 phones', inject(function($controller){
var scope = {},
ctrl = $controller('PhoneListCtrl', {$scope:scope});
expect(scope.phones.length).toBe(3);
}));
});
And here is the angular controller itself:
(function () {
var app = angular.module('phoneCtrlModule', []);
app.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.'}
];
});
})();
I've tried making $scope equal to this.phones but it doesn't work. I'm a little confused as to how you could reach PhoneListCtrl.phones inside of a karma test.
Upvotes: 0
Views: 61
Reputation: 347
keyword this here signifies the controller itself. you can use this.phones in the test case as
phoneCtrlModule.phones
alternative to it is:
var scope;
beforeEach(inject(function($rootScope,$Controller){
$scope=$rootScope.$new();
var ctrl = $controller('phoneCtrlModule', {
$scope: scope
});
}));
expect(scope.phones.length).toEqual(3);
Upvotes: 1