我目前正在使用Karma测试运行器进行我的Angular项目,使用茉莉花测试框架.它工作得很好,但我有一个问题:当一个对象比较失败时,生成的打印到控制台真的很难读,并且这些对象拥有的属性越来越难.例:
Expected spy spy to have been called with [ { currentCareMoment : { ID : 5, Description : 'Late namiddag (16-20)', StartHour : 16, EndHour : 20 }, previousCareMoment : { ID : 4, Description : 'Namiddag (14-16)', StartHour : 14, EndHour : 16 } } ] but actual calls were [ { currentCareMoment : { ID : 6, Description : 'Avond (20-24)', StartHour : 20, EndHour : 24 }, previousCareMoment : { ID : 5, Description : 'Late namiddag (16-20)', StartHour : 16, EndHour : 20 } } ].
Run Code Online (Sandbox Code Playgroud)
无论如何设置Jasmine(因为我认为Karma与它无关)打印对象更漂亮?只是一些换行和缩进已经是一个巨大的帮助.例:
Expected spy spy to have been called with [ {
currentCareMoment : {
ID : 5,
Description : 'Late namiddag (16-20)',
StartHour : 16,
EndHour : 20
},
previousCareMoment : {
ID : 4,
Description : 'Namiddag (14-16)',
StartHour : 14,
EndHour : 16
}
} ] but actual calls were [ {
currentCareMoment : {
ID : 6,
Description : 'Avond (20-24)',
StartHour : 20,
EndHour : 24
},
previousCareMoment : {
ID : 5,
Description : 'Late namiddag (16-20)',
StartHour : 16,
EndHour : 20
}
} ].
Run Code Online (Sandbox Code Playgroud)
Rem*_*ing 13
我的答案是基于茉莉花2.0.1.
方法1记录在茉莉花文档中.所以可能是推荐的.
然而,方法2更简单.
我最初虽然是所描述的创建一个自定义匹配这里.所以我从jasmine源代码中复制了toHaveBeenCalledWith匹配器并对其进行了修改,因此它可以打印出来:
var matchers = {
toBeCalledWith: function (util, customEqualityTesters) {
return {
compare: function() {
var args = Array.prototype.slice.call(arguments, 0),
actual = args[0],
expectedArgs = args.slice(1),
result = { pass: false };
if (!jasmine.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + jasmine.JSON.stringify(actual, undefined, 2) + '.');
}
if (!actual.calls.any()) {
result.message = function() {
return 'Expected spy ' + actual.and.identity() + ' to have been called with ' + JSON.stringify(expectedArgs, undefined, 2) + ' but it was never called.';
};
return result;
}
if (util.contains(actual.calls.allArgs(), expectedArgs, customEqualityTesters)) {
result.pass = true;
result.message = function() {
return 'Expected spy ' + actual.and.identity() + ' not to have been called with ' + JSON.stringify(expectedArgs, undefined, 2) + ' but it was.';
};
} else {
result.message = function() {
return 'Expected spy ' + actual.and.identity() + ' to have been called with ' + JSON.stringify(expectedArgs, undefined, 2) + ' but actual calls were ' + JSON.stringify(actual.calls.allArgs(), undefined, 2) + '.';
};
}
return result;
}
};
}
};
Run Code Online (Sandbox Code Playgroud)
然后,测试用例将使用我们的新匹配器:
describe('Test', function() {
beforeEach(function() {
jasmine.addMatchers(matchers);
});
it('should print pretty', function() {
var spy = jasmine.createSpy('spy');
spy({
currentCareMoment: {
ID: 5,
Description: 'Late namiddag (16-20)',
StartHour: 16,
EndHour: 20
},
previousCareMoment: {
ID: 4,
Description: 'Namiddag (14-16)',
StartHour: 14,
EndHour: 16
}});
expect(spy).toBeCalledWith({
currentCareMoment: {
ID: 6,
Description: 'Avond (20-24)',
StartHour: 20,
EndHour: 24
},
previousCareMoment: {
ID: 5,
Description: 'Late namiddag (16-20)',
StartHour: 16,
EndHour: 20
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
jasmine.pp但是,在实现这一过程中,我注意到jasmine使用该功能jasmine.pp进行漂亮的打印.所以我想我可以通过在我的测试文件上添加以下内容来覆盖它:
jasmine.pp = function(obj) {
return JSON.stringify(obj, undefined, 2);
};
Run Code Online (Sandbox Code Playgroud)
由于添加了其他答案的时间,karma-jasmine-diff-reporter中提供了漂亮的打印选项.我建议尝试一下 - 它是非常可配置的,并且与其他常见的测试记者一起为我工作很好.
最小配置如下:
reporters: ['jasmine-diff'],
jasmineDiffReporter: {
multiline: true,
pretty: true
},
Run Code Online (Sandbox Code Playgroud)
小智 5
我发现压倒jasmine.pp导致我的规范记者不再对实际与预期的差异进行颜色编码.
我的解决方案是将下面的片段添加到它自己的文件中,将其加载到karma.conf中,然后将自定义匹配器(使用下划线断言深度相等)添加到报告器的配置中,该配置器在控制台中生成颜色编码的差异(业力) -jasmine-diff-reporter)
//This will run before all of our specs because it's outside of a describe block
beforeEach(function() {
var objectMatcher = {
toEqualObject: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var result = {};
result.pass = _.isEqual(actual, expected);
if (result.pass) {
result.message = "Expected \n" + JSON.stringify(actual, null, 2) + "\n not to equal \n" + JSON.stringify(expected, null, 2) + "\n";
} else {
result.message = "Expected \n" + JSON.stringify(actual, null, 2) + "\n to equal \n" + JSON.stringify(expected, null, 2) + "";
}
return result;
}
};
}
};
jasmine.addMatchers(objectMatcher);
});Run Code Online (Sandbox Code Playgroud)
现在我可以通过调用以下方式在控制台中获得这样的输出expect(foo).toEqualObject(bar):
弄清楚如何使用茉莉花间谍这项工作留给读者练习.
| 归档时间: |
|
| 查看次数: |
5974 次 |
| 最近记录: |