So we have karma-coverage setup and what I believe to be working, but the reports generated don't seem to be taking all the unit tests into account. We get a lot of "statement not covered" or "function not covered" in the controllers, services, etc. where we have exercised the lines of code within tests.
We are just implementing this ( and unit tests ), so I'm sure there are some things we are missing, so any help to point us in the right direction would be appreciated.
Here is a snippet incase we are doing something wrong in the testing piece:
Controller
(function(){
var simpleMod = angular.module('simpleMod', []);
simpleMod.controller('simpleController', function($scope){
$scope.test = "A";
$scope.TestMethod = function()
{
$scope.test = "B";
};
});
})();
Test
describe('Test Suite', function () {
var scope;
var simpleController;
beforeEach(module('simpleMod'));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
simpleController = $controller('simpleController', {
$scope: scope
});
}));
it('Test 001', function() {
spyOn(scope, 'TestMethod').and.callThrough();
expect(scope.test).toEqual('A');
scope.TestMethod();
expect(scope.TestMethod).toHaveBeenCalled();
expect(scope.test).toEqual('B');
});
});
The test runs and passes but when we look at the coverage report, it says for the line "$scope.test = "A";" for example, that it's not covered, same for the method. Does the test as setup truly not cover those items? If so, can help be provided as to how we are testing incorrectly? Thanks.
Finally found the problem. We are using Grunt to process everything and we had added concat and minify tasks to the flow. Those came later in the process and the coverage report was showing the last version of the run which was likely the minified version and thus reporting incorrectly.