Karma [Jasmine] - 如何将代码与测试用例分开

时间:2017-03-29 13:07:39

标签: javascript jasmine karma-runner

是否有使用Jasmine将代码与测试分开的标准方法?

== functions.js

function sample() {
    return true;
}

== functions.spec.js

describe('functions', function () {

    it('sample', function () {
        expect(sample()).toEqual(true);
    });

});

== karma.config.js

module.exports = function (config) {
    config.set({
        basePath: __dirname + '/../public/',
        frameworks: [
            'jasmine'
        ],
        browsers : ['PhantomJS'],
        client: {
            jasmine: {
                stopOnFailure: true
            }
        },
        files: [
            'js/**/*.spec.js'
        ]
    });
};

提前致谢!

我试图使用' browserify'没有成功:

== karma.config.js

browserify: {
        debug: true,
        plugin: ['proxyquireify/plugin'],
        extensions: ['.js']
    },
    preprocessors: {
        'js/**/*.spec.js': ['browserify']
    }

== functions.spec.js

var source = require('./functions.js');
...
expect(source.sample()).toEqual(true);

1 个答案:

答案 0 :(得分:0)

原始文件中的一处小改动有助于:

== functions.js

this.sample = function() {
    return true;
};