I have a controller with a local variable
function IndexCtrl($scope) {
var pagesById = [];
loadPages();
// snip
function loadPages() {
// pagesById gets populated
}
// snip
}
I'd like to test that pagesById is correctly populated but I'm not sure how to get at it from my it(). I don't need this variable to be in the $scope, it's just an intermediate set of information, so if I can avoid adding it to $scope that would be ideal.
it('scope.pages should populated based on pages data.', function() {
$httpBackend.flush();
expect(pagesById).toEqualData(mock_page_results);
});
gives me
ReferenceError: pagesById is not defined
Do I have any other options besides attaching it to $scope?
The HttpClientTestingModule allows you to easily mock HTTP requests by providing you with the HttpTestingController service.
detectChanges() tells Angular to run change-detection. Finally! Every time it is called, it updates data bindings like ng-if, and re-renders the component based on the updated data. Calling this function will cause ngOnInit to run only the first time it is called.
Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.
In your jasmine spec, first create the controller:
var ctrl;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('myController', {
$scope: scope
});
}));
Then you can access its properties by doing ctrl.pagesById
. Of course, rather than doing var pagesById
you'll need to use this.pagesById
in your controller.
Here is my way for testing local variables in Angular methods:
//------- Function for getting value of local variables
function getLocalVariable(sLocalVarName, obj, method, args){
method = method.toString();
let changedMethod = method.substring(0, method.lastIndexOf("}")) + ";" + "return " + sLocalVarName + "}";
eval(' changedMethod = ' + changedMethod);
return changedMethod.call(obj, args)
}
//----------- service
class assignStuffService {
getAvaliableStuff(shift) {
const params = {
agency_id: 0 ,
start_time: shift.data.shift.start_time,
end_time : shift.data.shift.end_time
};
}
}
//----------- part of spec
it('should set values to "params" object props', () => {
let shift = {
data:{
shift:{
start_time:'',
end_time:''
}
}
};
let params = getLocalVariable('params',
assignStuffService,
assignStuffService.getAvaliableStuff,
shift);
expect(params).toEqual({
agency_id: 0 ,
start_time: shift.data.shift.start_time,
end_time : shift.data.shift.end_time
});
});
Here is my way for testing local variables in Angular methods:
//------- Function for getting value of local variables
function getLocalVariable(sLocalVarName, obj, method, args){
method = method.toString();
let changedMethod = method.substring(0, method.lastIndexOf("}")) + ";" + "return " + sLocalVarName + "}";
eval(' changedMethod = ' + changedMethod);
return changedMethod.call(obj, args)
}
//----------- service
class assignStuffService {
getAvaliableStuff(shift) {
const params = {
agency_id: 0 ,
start_time: shift.data.shift.start_time,
end_time : shift.data.shift.end_time
};
}
}
//----------- part of spec
it('should set values to "params" object props', () => {
let shift = {
data:{
shift:{
start_time:'',
end_time:''
}
}
};
let params = getLocalVariable('params',
assignStuffService,
assignStuffService.getAvaliableStuff,
shift);
expect(params).toEqual({
agency_id: 0 ,
start_time: shift.data.shift.start_time,
end_time : shift.data.shift.end_time
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With