因此,jasmine 中 jQuery 值的默认漂亮打印机不是很漂亮(它将它打印为一个对象,列出所有可用的方法)。如果将其打印为数组会更好。
在 jQuery 对象的情况下,我可以重写jasmine.PrettyPrinter.prototype.format
以给出特定的指令,value instanceof jQuery
或者重写jasmine.isArray_
以返回 true,但这两者看起来都像是 hack。
有没有更自然的方式来扩展 jasmine pretty 打印机?
因此,jasmine 中 jQuery 值的默认漂亮打印机不是很漂亮(它将它打印为一个对象,列出所有可用的方法)。如果将其打印为数组会更好。
在 jQuery 对象的情况下,我可以重写jasmine.PrettyPrinter.prototype.format
以给出特定的指令,value instanceof jQuery
或者重写jasmine.isArray_
以返回 true,但这两者看起来都像是 hack。
有没有更自然的方式来扩展 jasmine pretty 打印机?
@James deBoer 解决方案是正确的想法,但我不得不对其进行一些修改以使其正常工作:
jQuery.fn.jasmineToString = function() {
this[0].outerHTML;
};
this
是 jQuery 对象;[0]
给你第一个 DOM 元素(因为 jQuery 对象是匹配元素的集合),并且 DOM 属性是outerHTML
(注意大写)。
(@James 很有可能已经定义了自己的 jQuery.outerHtml() 函数。有关更多信息,请参阅这个 StackOverflow 问题)。
jasmine.pp 将查找要在对象上定义的方法“jasmineToString”。
我像这样扩展了我的 jQuery 对象:
jQuery.fn.jasmineToString = function() {
return this.outerHtml();
};