【问题标题】:How to run basic test using karma (testacular)如何使用 karma (testacular) 运行基本测试
【发布时间】:2013-07-12 12:25:20
【问题描述】:

我最近一直在玩 jasmine,以便开始在我的项目中加入测试。 一切似乎都很好,直到我想用 karma(以前的 Karma)自动化工作流程。

在我的 src 目录中,我有一个简单的计算器对象和几个简单的方法: 函数计算器() { };

var current = 0;

Calculator.prototype.add = function() {
if (arguments.length < 2) { 
    // we only have one arguments
    current += arguments[0];
    return current;
} else { 
    // more than one arguments
    for( var i in arguments ) 
        current += arguments[i];
    return current;
}
};

Calculator.prototype.substract = function() {
var currentValue = arguments[0];

for ( var i = 1; i < arguments.length; i++ ) 
    currentValue -= arguments[i];   
return currentValue;
};

Calculator.prototype.reset = function() {
window.current = 0;
}

然后在我的规范文件中,我确实有以下内容(所有测试都通过了,没有 Karma):

var 计算器 = new Calculator();

describe('Calculator', function() {
beforeEach(function() {
    window.current = 0;
});

describe('When adding numbers', function() {
    it('should store the current value at all times', function() {
        expect(window.current).toBeDefined();
    });

    it('should add numbers', function() {
        expect(window.calculator.add(5)).toEqual(5);
        expect(window.calculator.add(10)).toEqual(15);
    });

    it('should add any number of numbers', function() {
        expect(calculator.add(1, 2, 3)).toEqual(6);
        expect(calculator.add(1, 2)).toEqual(9);
    })
});

describe('When substracting numbers', function() {
    it('should substract any number of numbers', function() {
        expect(calculator.substract(5, 3)).toEqual(2);
    });
});

it('should reset the current value back to zero', function() {
    window.current = 20;
    calculator.reset();

    expect(window.current).toEqual(0);

    calculator.add(5);
    calculator.add(20);
    expect(window.current).toEqual(25);

    calculator.reset();
    expect(window.current).toEqual(0);
});
});

当我运行 karma start 时,我得到以下信息: Chrome 28.0 (Mac) 错误 未捕获的 ReferenceError:未定义计算器 在 /Users/roland/learning/jasmine/jasmine-standalone-1.3.1/spec/calculator_spec.js:1

感谢您的帮助!

【问题讨论】:

    标签: javascript testing jasmine karma-runner


    【解决方案1】:

    您似乎没有加载具有Calculator 的文件,或者它可能是在规范文件之后加载的。在您的 Karma 配置文件中,您需要执行以下操作:

    files = [
      'path/to/calculator.js',
      JASMINE,
      JASMINE_ADAPTER,   
      'path/to/calculator_spec.js'
    ];
    

    【讨论】:

    • 是的,就是这样!我只是在看规范文件夹!谢谢!
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2019-02-17
    • 2015-09-11
    相关资源
    最近更新 更多