I am trying to write a test case for if conditions within a loop, I however do not have it as part of a filter. Is there any way I can test the if conditions within a loop. My test case does not execute this line of code at all.
Below is my controller code
$scope.init = function()
{
for (var i = 0; i < $scope.tempdata.length; i++) { //mapping of the status to text
if ($scope.tempdata[i].Status == 'U') {
$scope.statusText = 'text1';
}
if ($scope.tempdata[i].Status == 'A' || $scope.tempdata[i].Status == 'W') {
$scope.statusText = 'text2';
}
if ($scope.tempdata[i].Status == 'F') {
$scope.statusText = 'text3';
}
if ($scope.tempdata[i].Status == 'P') {
$scope.statusText = 'text4';
}
if ($scope.tempdata[i].Status == 'E') {
$scope.statusText = 'text5';
}
if ($scope.tempdata[i].Status == 'S') {
$scope.statusText = 'text6';
}
}
}
Below is my test case
it('should set the status', function() {
scope.responseSet = true;
var mockTempData =[{'Status': 'F'}];
scope.tempdata = mockTempData
scope.init();
expect(scope.statusText).toBe(text3);
});
When i run karma my test case fails with Expected '' to be text3.
It would be easier to tell if you show how you are "injecting" scope into your controller test. However, I find it very helpful to debug my unit tests to carefully step through them and make sure everything is as I expect each step along the way.
You should also have your last line be
Note that text3 in your test code should be changed to 'text3' since this is a string in your actual controller, not a variable name.