After creating an application using AngularJS and additional tools based on bootstrap, I am now facing the challenge of testing everything. However, there seems to be a problem with Karma and Jasmine as an exception is thrown when running the tests.
The issue revolves around a TypeError where the object is not recognized as a method (apologies for the French language in my console).
Below is an excerpt from my karma.conf.js file:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'src/test/lib/angular/angular.js',
'src/test/lib/angular/angular-mocks.js',
'src/test/lib/angular/angular-route.min.js',
'src/test/lib/jquery-1.9.1.min.js',
'src/main/webapp/resources/js/pages/*.js',
'src/main/webapp/resources/js/pages/survey/*.js',
'src/main/webapp/resources/js/pages/billing/*.js',
'src/test/*Spec.js',
'src/test/survey/*Spec.js',
'src/test/billing/*Spec.js',
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['IE', 'Firefox'],
junitReporter: {
outputFile: 'unit.xml',
suite: 'unit'
}
});
};
In both my JSP and JS files, I utilize the bootstrap-select library by Silvio Moreto:
The code snippet that triggers the exception is as follows:
angular.module("searchSurveyApp", [])
.controller("searchSurveyController" , function($scope, $http,$window) {
...
$("#selectpicker").selectpicker(); // <= launches exception in karma
Lastly, here is the JavaScript test code:
describe('Unit: searchSurveyController', function() {
// Load the module with MainController
beforeEach(module('searchSurveyApp'));
var ctrl, scope, http;
// inject the $controller and $rootScope services
// in the beforeEach block
beforeEach(inject(function($controller, $rootScope, $http) {
// Create a new scope that's a child of the $rootScope
scope = $rootScope.$new();
http = $http;
// Create the controller
ctrl = $controller('searchSurveyController', {
$scope: scope,
$http: http
});
}));
it('should write hello',
function() {
console.log("hello");
});
});
If I remove the line: $("#selectpicker").selectpicker(); from the code, the test passes without any issues. Therefore, my question is how can I effectively test this type of code?
I appreciate any assistance in advance as I am currently stuck on this matter.