用 Karma 测试

Testing with Karma

我是 Karma 的初学者,我无法对我的项目进行测试。 我试过这个: http://blog.codeship.com/mocha-js-chai-sinon-frontend-javascript-code-testing-tutorial/ 该示例适用于 "html page testing",但我无法使其适用于 start karma.conf.js(我得到:Executed 0 of 0 ERROR)

这是我的 karma.conf.js 文件:

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
    });
  };

这是我要测试的文件:(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;
    }
]);

这是我的测试文件:

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");
    });

  });

});

**编辑: 非常感谢,现在可以使用了**

您似乎缺少引用您的测试规范

你有

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

应该是

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

您还需要(根据 ivarni 的评论)更改语法

来自

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

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

为你的 beforeEach 工作

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