'Karma shows code as uncovered although I have written test cases
I 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. I get a lot of "statement not covered" or "function not covered" in the controllers, services, etc. where I have exercised the lines of code within tests.
Here is a snippet incase we are doing something wrong in the testing piece:
Controller
var simpleMod = angular.module('simpleMod', []);
simpleMod.controller('simpleController', function($scope){
$scope.test = "A";
$scope.TestMethod = function()
{
$scope.test = "B";
};
});
})();
Test
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');
});
});
Below is my karmconfig file. Can you please tell me what I did wrong?
module.exports = function(config) {
config.set({
plugins:[
require('karma-coverage'),
require('karma-phantomjs-launcher'),
require('karma-chrome-launcher'),
require('karma-jasmine'),
require('karma-junit-reporter'),
require('karma-ng-json2js-preprocessor')
],
basePath: '',
frameworks: ['jasmine'],
files: [
'build/tests/**/test_*.js'
'build/js/controllers/**/*.js',
],
exclude: [
],
preprocessors: {
'**/*.json': ['ng-json2js'],
'**/*.js': ['coverage']
},
coveragerReport: {
dir: '../../coveraqge-results/karma
reporters: [
{type: 'html', subdir:'coverage-html'},
{type: 'text"},
{type: 'text-summary'}
}
coverageIstanbulReporter: {
reports: ['html', 'lcovonly', 'teamcity'],
dir: '../install/coverage-results/karma',
fixWebpackSourcePath: true,
'report-config': {
html: {
subdir: 'coverage-html'
},
lcovonly: {
file: 'coverage-lcov/coverage.lcov'
}
}
},
junitReporter: {
outputFile: 'test-results.xml',
outputDir: 'test-results/karma'
}
reporters: ['progress', 'junit', 'coverage'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
singleRun: true,
ngJson2JsPreprocessor: {
stripPrefix: 'app/config/',
prependPrefix: 'served/'
}
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
