I'm trying to test my angular rest calls with karma and jasmine. for starters I just want to test my Auth Service and do a simple login.
describe('Auth Service', function () {
var httpBackend,
service;
beforeEach(function () {
angular.mock.module('mean');
inject(function ($httpBackend, Auth) {
httpBackend = $httpBackend;
httpBackend.expect('POST', '/auth/v1/login', { email: '[email protected]', password : 'password' }).respond({
response: {
success: true
}
});
service = Auth;
});
});
it('should login.', function () {
var result;
service.login('[email protected]', 'password').then(function (response) {
result = response;
});
httpBackend.flush();
// TODO
});
});
I know that this code won't work as is, but unfortunately I don't even get as far as to check if the rest call is being executed properly. I get the following exception:
Error: Unexpected request: GET internationalization/locale-en.json
Expected POST /auth/login
locale-en.json is one of my internationalization files which I didn't reference anywhere in this Test (or even in the Auth Service).
here's the implementation of the login function in the Auth Service:
function login(email, password) {
var deferred = $q.defer();
$http.post('/auth/v1/login', {email: email, password: password})
.success(function (t) {
$localStorage.token = t;
deferred.resolve(t);
})
.error(function (err) {
deferred.reject(err);
});
return deferred.promise;
}
Where does the seemingly random GET call to the json come from and what am I doing wrong here?
edit: my karma.conf.js
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine',/* 'requirejs',*/ 'browserify'],
plugins: [
//'karma-requirejs',
'karma-jasmine',
'karma-browserify',
'karma-chrome-launcher'
],
files: [
'public/bower_components/angular.js'
// other bower_components
'node_modules/requirejs/require.js',
'node_modules/karma-requirejs/lib/adapter.js',
'public/app.js',
'public/config/*.js',
'public/controllers/**/*.js',
// services, directives, filters, internationalization just like the ctrl's
'public/tests/*.js'
},
exclude: [],
preprocessors: {
'public/tests/*.js': [ 'browserify' ]
},
// the rest is standard karma config
}) };
I commented require.js out since I always get the error
http://requirejs.org/docs/errors.html#mismatch
at /home/markus/git/dealscreening/node_modules/requirejs/require.js:140
which I can't seem to fix. I also added a test-main.js file for require.js (otherwise I couldn't run karma tests at all)
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
console.log('requireJS:', file);
}
}
}
requirejs.config({
baseUrl: '/base',
paths: {},
deps: tests,
callback: window.__karma__.start
});
Probably my karma config is somehow wrong, unfortunately I don't really know how to configure it correctly since none of the tutorials I went through seem to work in my project.
github example project:
https://github.com/markus138/karmatest
to test it:
npm install
cd public
bower install
cd ..
npm test
versions: angular 1.4.4 karma 0.13.9 jasmine 2.3.4
Error: Unexpected request: GET views/home/home.html- Markus