如您所见,我的测试用例有重复部分。
我该如何处理?只写一个常用的函数来重用?
有mocha
处理此问题的方法吗?
it('test case 1', function() {
var target = {
a: 1,
b: 1
};
var source = {
c: 1
};
expect(deepMerge(target, source)).to.deep.equal({a: 1, b: 1, c: 1});
//dulplicate
expect(target).to.eql(target);
expect(source).to.eql(source);
});
it('test case 2', function() {
var target = {
a: 1,
b: 1
};
var source = {
a: 2,
c: 1
};
expect(deepMerge(target, source)).to.deep.equal({a: 2, b: 1, c: 1});
//dulplicate
expect(target).to.eql(target);
expect(source).to.eql(source);
});
//more test cases also have the duplicate part.
答案 0 :(得分:0)
您可以将所有重复的期望案例放在某个函数中,并在每个it
块中调用该函数;
function duplicateTests(target, source) {
expect(target).to.eql(target);
expect(source).to.eql(source);
};
it('test case 1', function() {
var target = {
a: 1,
b: 1
};
var source = {
c: 1
};
expect(deepMerge(target, source)).to.deep.equal({a: 1, b: 1, c: 1});
duplicateTests(target, source);
});
it('test case 2', function() {
var target = {
a: 1,
b: 1
};
var source = {
a: 2,
c: 1
};
expect(deepMerge(target, source)).to.deep.equal({a: 2, b: 1, c: 1});
duplicateTests(target, source);
});
//more test cases also have the duplicate part.