我对测试很陌生,所以这个问题是关于最佳实践以及如何编写应该的。我使用Karma与Mocha和Chai一起测试Angular.js应用程序。
我目前正在测试一个函数,该函数计算按特定顺序排列字母组合的方式数。它遵循一种模式来从辅音集或元音集中提取字母。
这些是我目前的测试:
describe("should count the number of combinations correctly", function() {
describe("with 2-item arrays", function() {
beforeEach(function(){
scope.vowels = ['a', 'e'];
scope.consonants = ['b', 'c'];
})
it("with 2 letters in pattern", function() {
scope.pattern = 'CV';
expect(scope.combinationCounter()).to.equal(4);
});
it("with 3 letters in pattern", function() {
scope.pattern = 'CVC';
expect(scope.combinationCounter()).to.equal(8);
});
it("with 4 letters in pattern", function() {
scope.pattern = 'CVCV';
expect(scope.combinationCounter()).to.equal(16);
});
});
describe("with 3-item arrays", function() {
beforeEach(function(){
scope.vowels = ['a', 'e', 'i'];
scope.consonants = ['b', 'c', 'd'];
})
it("with 2 letters in pattern", function() {
scope.pattern = 'CV';
expect(scope.combinationCounter()).to.equal(9);
});
it("with 3 letters in pattern", function() {
scope.pattern = 'CVC';
expect(scope.combinationCounter()).to.equal(27);
});
it("with 4 letters in pattern", function() {
scope.pattern = 'CVCV';
expect(scope.combinationCounter()).to.equal(81);
});
});
});
这些测试有效,并且它们给我提供了有用的错误信息,但我不能感觉我做了一些不必要的重复,因为我基本上用不同的值执行相同的测试。
有没有办法编写这些维护错误消息结构的测试,但并不要求我写出来
it("with x letters in pattern", function() {
scope.pattern = 'whatever';
expect(scope.combinationCounter()).to.equal(y);
});
每一次?
我想在没有庞大测试文件的情况下测试大量案例,但要保持错误消息的可读性。
@Stas给了我正确的答案,即使用带有对象的循环来保存不同的测试用例。但是,由于他的答案是用lodash写的,我没有使用,我已经在下面提供了我的最终循环代码供参考。
我在这里循环的scenarios
对象与@ Stas'中的对象相同。示例
for (var x in scenarios) {
var vowels = scenarios[x].arrays.vowels;
var consonants = scenarios[x].arrays.consonants;
var v = vowels;
var c = consonants;
describeStuff();
}
function describeStuff(){
describe("with " + x, function(){
setLetters(v,c);
});
}
function setLetters(vowels, consonants){
describe("("+vowels + ' & ' + consonants + "),", function(){
beforeEach(function(){
scope.vowels = vowels;
scope.consonants = consonants;
});
innerLoop();
});
}
function innerLoop(){
for (var y in scenarios[x].combinations) {
var combinations = scenarios[x].combinations;
var pat = scenarios[x].combinations[y].pattern;
var res = scenarios[x].combinations[y].result;
setResults(pat, res);
}
}
function setResults(p, r){
var pattern = p;
var result = r;
it("with " + p.length + " letters in pattern (" + p + ")", function(){
scope.pattern = pattern;
expect(scope.combinationCounter()).to.equal(result);
});
}
我必须使用四个相互调用的函数链来编写循环,因为在for in
循环中使用Mocha的回调语法只会将最终的测试用例保存到函数变量中。在循环外定义函数然后在内部调用它们可以解决这个问题。
答案 0 :(得分:3)
您可以为每个循环创建场景对象并包装执行(在此示例中我使用lodash):
describe("should count the number of combinations correctly", function () {
var scenarios = {
"2-item arrays": {
arrays: {
vowels: ['a', 'e'],
consonants: ['b', 'c']
},
combinations: [
{pattern: "CV", result: 4},
{pattern: "CVC", result: 8},
{pattern: "CVCV", result: 16}
]
},
"3-item arrays": {
arrays: {
vowels: ['a', 'e', 'i'],
consonants: ['b', 'c', 'd']
},
combinations: [
{pattern: "CV", result: 9},
{pattern: "CVC", result: 27},
{pattern: "CVCV", result: 81}
]
}
};
_.forEach(scenarios, function (scenario, key) {
describe("with " + scenario.key, function () {
beforeEach(function () {
scope.vowels = scenario.arrays.vowels;
scope.consonants = scenario.arrays.consonants;
});
_.forEach(scenario.combinations, function(combination) {
it("with " + combination.pattern.length + " letters in pattern", function() {
scope.pattern = combination.pattern;
expect(scope.combinationCounter()).to.equal(combination.result);
});
})
});
});
});
通过这种方式,您可以添加更多场景,而无需为每个组合复制describe()/ beforeEach()/ it(),它将生成相同的消息。