0
votes

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

1
It would be helpful to see your full karma config file and test file, I suspect you are injecting a module in the beforeEach or somewhere that is requiring this file and hence causing a issue. - James
I added my karma config. The more I look at my config, the more I'm sure that I'm doing something completely wrong, I just can't figure out what exactly - Markus
Look at your angular module .run() call and see if the request in question is being executed in there. - TomSlick
I dont have a call in .run(), but in .config(). Just for testing I commented it out. Now I get the same error, but with html views Error: Unexpected request: GET views/home/home.html - Markus
Do you have this code in a github repo I can't seem to figure out where the issue is coming from and it would be helpful if I could run it locally. - James

1 Answers

0
votes

You probably reference the main module in your test. So usually you have a router defined to it with different states. Maybe it tries to load the html because you defined it like that. You can propably ignore the call by responsing a 400 on the expectation. Greetings