lundi 29 juin 2015

Unable to load AngularJS dependencies in Jasmine + Karma

Sorry that this question might seem newbie but I have just started learning on unit testing on my own and I have been struggling to get unit testing set up in Jasmine/Karma + AngularJS (Following John Papa's style). It has been bugging me for a couple days now.

(function() {
    var myModule = angular.module("myApp").controller("myCtrl", myCtrl);

    myCtrl.$inject = [];

    function myCtrl() {
        var vm = this;
        vm.sum = sum;

        function sum(x, y) {
            return x + y;
        }
    }
})();

controller.js

describe("Calculate sum", function(){
    beforeEach(module('myApp'));

    var controller;
    beforeEach(inject(function($controller) {
        controller = $controller('myCtrl', {}); 
    }));

    it("myCtrl is defined", function(){
        expect(controller).toBeDefined(); //
    })
});

controller.spec.js

Result of the test:

FAIL. Expect undefined to be defined

My file structure:

Karma Configuration below:

// Karma configuration
// Generated on Mon Jun 22 2015 18:06:04 GMT+0800 (Malay Peninsula Standard Time)

module.exports = function(config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',


        // frameworks to use
        // available frameworks: http://ift.tt/1ft83uu
        frameworks: ['jasmine'],


        // list of files / patterns to load in the browser
        files: [
            'js/angular.min.js',
            'js/angular-mocks.js',
            'modules/myCtrl/controller.js',
            'tests/**/*.test.js'
        ],


        // list of files to exclude
        exclude: [
            // 'js/angular-animate.min.js',
            // 'js/angular-sanitize.min.js',
            // 'js/angular-ui-router.js',
            // 'js/angular.js'
        ],


        // preprocess matching files before serving them to the browser
        // available preprocessors: http://ift.tt/1gyw6MG
        preprocessors: {},


        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: http://ift.tt/1ft83KQ
        reporters: ['progress'],


        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        // logLevel: config.LOG_INFO,
        logLevel: config.LOG_DISABLE,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: http://ift.tt/1ft83KU
        browsers: ['PhantomJS'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false
    });
};

Would really appreciate any help/guidance on the direction to identify the error. *The code in the controller is working in the app. I took a working code and tried to do unit testing on it.

Aucun commentaire:

Enregistrer un commentaire