我有以下函数
function trim(value) {
if (typeof value === 'string') {
if (String.prototype.trim) {
value = value.trim();
} else {
value = value.replace(/^\s+|\s+$/g, '');
}
return value;
}
} 我正在为它编写一个单元测试,以确保在调用trim时,如果可用,也会调用本机String.prototype.trim。我正在尝试使用spy来确保它被调用
var Util = require('test/util/methods');
it('should use native trim', function() {
var spy = sinon.spy(String.prototype, 'trim');
Util.trim('test string ');
expect(spy.calledOnce).toEqual(true);
expect(Util.trim('test string ')).toEqual('test string');
spy.restore();
});但我觉得我应该做的是,当trim被调用时,我应该检查String.prototype.trim也被调用了。
我该怎么做呢?如果任何人有任何指示,请也建议,因为我想得到它的测试方面,我可以尽我所能
谢谢
发布于 2017-01-24 16:57:13
所以只调用trim一次,然后使用两个expect:
it('should use native trim', function() {
var spy = sinon.spy(String.prototype, 'trim');
expect(Util.trim('test string ')).toEqual('test string');
expect(spy.calledOnce).toEqual(true);
spy.restore();
});https://stackoverflow.com/questions/41823661
复制相似问题