简体   繁体   English

如何使用业力运行基本测试(Testacular)

[英]How to run basic test using karma (testacular)

I've been playing with jasmine recently in order to start incorporating tests in my projects. 我最近一直在与茉莉花玩耍,以便开始将测试合并到我的项目中。 All seemed to work fine until, I wanted to automate the workflow with karma (previously Karma). 在我想使用业力(以前是业力)使工作流程自动化之前,一切似乎都运转良好。

In my src directory, I have simple Calculator Object with a couple of simple methods: function Calculator() { }; 在我的src目录中,我有一个简单的Calculator Object和一些简单的方法:function Calculator(){};

var current = 0; 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;
}

Then in my spec file, I do have the following ( all tests passes without Karma ): 然后,在我的规格文件中,我确实有以下内容(所有测试通过时都没有Karma):

var calculator = new Calculator(); var Calculator = 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);
});
});

When I run karma start, I get the following: Chrome 28.0 (Mac) ERROR Uncaught ReferenceError: Calculator is not defined at /Users/roland/learning/jasmine/jasmine-standalone-1.3.1/spec/calculator_spec.js:1 运行业力启动时,得到以下信息:Chrome 28.0(Mac)错误未捕获的参考错误:在/Users/roland/learning/jasmine/jasmine-standalone-1.3.1/spec/calculator_spec.js:1中未定义计算器

Thank you for the help! 感谢您的帮助!

It seems like you're not loading the file that has Calculator or perhaps it's being loaded after the spec file. 似乎您没有加载包含Calculator的文件,或者似乎是在spec文件之后加载的。 In your Karma configuration file, you'll want to do something like this: 在您的Karma配置文件中,您需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM