I'm trying to access the controller's properties in the jasmine tests but I'm having issues setting the test up.
Here's the controller:
adminApp.controller('AdminClientController', [function() {
var adminClient = this;
adminClient.name = 'Client Name';
adminClient.sites = [
{
id: "site1",
domain: "myschool.com",
pages: [
{
id: 1,
name: "HOME"
},
{
id: 2,
name: "FORM"
}
]
}
]
}]);
Here is the spec:
describe('AdminClientController', function() {
beforeEach(module('adminClientApp'));
var $scope, $controller;
beforeEach(inject(function(_$controller_, _$rootScope_){
$rootScope = _$rootScope_;
$controller = _$controller_;
$scope = $rootScope.$new();
}));
describe('site pages', function() {
beforeEach(function() {
//$scope = {};
controller = $controller('AdminClientController as adminClient', {
$scope: $scope });
});
it('should create a site model with 2 pages', function() {
expect(controller.adminClient.sites[0].pages.length).toBe(2);
});
});
});
The error:
TypeError: Cannot read property 'sites' of undefined
Previously I tried initializing the $scope (even though I'm not explicitly using it in the controller) and didn't use the contoller as syntax.
beforeEach(function() {
$scope = {
adminClient: {
sites: [
{
id: "site1",
domain: "myschool.com",
pages: [
{
id: 1,
name: "HOME"
},
{
id: 2,
name: "FORM"
}
]
}
]
}
};
controller = $controller('AdminClientController', {
$scope: $scope });
});
I'm just starting out with the testing and want to use it early on or else it won't happen later. The other SO questions I found didn't help me out.
Thanks!