2
votes

I'm a beginner with Karma, and I can't make a test work on my project. I tried with this : http://blog.codeship.com/mocha-js-chai-sinon-frontend-javascript-code-testing-tutorial/ The exemple works with the "html page testing", but I can't make it work with start karma.conf.js (I got : Executed 0 of 0 ERROR)

Here is my karma.conf.js file :

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai'],
    files: [
        'node_modules/angular/angular.js',
        'node_modules/angular-mocks/angular-mocks.js',
        'src/client/api/category/category.model.js'
        **'test.js'** (edit)

    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
    });
  };

Here is the file I want to test : (src/client/api/category/category.model.js)

angular.module("aq.kiosk.api.category.models", [])
.factory("CategoryModel", [
    function () {

        var CategoryModel = function (data) {

            this.id = "";
            this.name = "";
            this.description = "";
            this.mediaUrls = [];
            this.kind = "";
            this.childCount = "";
            this.issueCount = "";
            this.issues = [];

            if (angular.isDefined(data)) {
                this.parse(data);
            }
        };

        CategoryModel.prototype.parse = function (data) {
            if (data) {
                var self = this;
                angular.forEach(data, function (value, key) {
                    self[key] = value;
                });
            }
        };

        return CategoryModel;
    }
]);

And here is my test file :

describe("categorymodel", function() {

var data = {
            childCount: "0",
            description: "",
            id: "16",
            issueCount: 0,
            kind: "",
            mediaUrls: [],
            name: "Lorem Ipsum"
        };

beforeEach(module('aq.kiosk.api.category.models'));
beforeEach(inject(function (_CategoryModel_) {
CategoryModel = _CategoryModel_;

}));

  describe("constructor", function(){
    it('assigns a name', function () {
      expect(CategoryModel(data)).to.have.property('name', "Lorem Ipsum");
    });

  });

});

**EDIT : Thank you very much, now it works **

1
Where's the code that creates the module? angular.module("aq.kiosk.api.category.models") assumes it is already created. If you're trying to create the module in your category.model.js file then the syntax would be angular.module("aq.kiosk.api.category.models", []). If the module is created by another file, how is that file included? - ivarni

1 Answers

0
votes

It seems you are missing referencing your test spec

You have

files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'src/client/api/category/category.model.js'
],

It should be

files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'src/client/api/category/category.model.js',
    'specs/category.spec.js'
],

Also you will need to (per ivarni's comment) change your syntax

from

angular.module("aq.kiosk.api.category.models")

to

angular.module("aq.kiosk.api.category.models", [])

for your beforeEach to work

beforeEach(module('aq.kiosk.api.category.models'));