【问题标题】:Jasmine: What's the easiest way to define a custom pretty printer for jQuery objects?Jasmine:为 jQuery 对象定义自定义漂亮打印机的最简单方法是什么?
【发布时间】:2011-12-13 14:43:48
【问题描述】:

所以 jasmine 中 jQuery 值的默认漂亮打印机不是很漂亮(它将它打印为一个对象,列出所有可用的方法)。如果只是将它打印为数组会更好。

我可以覆盖 jasmine.PrettyPrinter.prototype.format 以在 value instanceof jQuery 的情况下给出具体说明,或者覆盖 jasmine.isArray_ 以为 jQuery 对象返回 true,但这两者看起来都像是 hack。

有没有更自然的方式来扩展 jasmine Pretty 打印机?

【问题讨论】:

    标签: javascript jquery testing jasmine jasmine-jquery


    【解决方案1】:

    @James deBoer 解决方案是正确的想法,但我必须对其进行一些修改才能使其正常工作:

    jQuery.fn.jasmineToString = function() {
      this[0].outerHTML;
    };
    

    this 是 jQuery 对象; [0] 为您提供第一个 DOM 元素(因为 jQuery 对象是匹配元素的集合),DOM 属性为 outerHTML(注意大写)。

    (@James 很有可能已经定义了自己的 jQuery.outerHtml() 函数。请参阅this StackOverflow question 了解更多信息)。

    【讨论】:

      【解决方案2】:

      jasmine.pp 将查找要在对象上定义的方法“jasmineToString”。

      我像这样扩展了我的 jQuery 对象:

      jQuery.fn.jasmineToString = function() {
        return this.outerHtml();
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        • 2021-05-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多