【问题标题】:Why are two html elements with different attributes deepStrictEqual为什么两个属性不同的html元素是deepStrictEqual
【发布时间】:2020-03-22 14:38:26
【问题描述】:

创建两个 html div 元素,给一个 class="foo" 的属性。

DeepStrictEqual 断言它们是相等的。

为什么?

const jsdom = require("jsdom");
const {JSDOM} = jsdom;
var assert = require('assert');
global.document = new JSDOM(`<!DOCTYPE html>`).window.document;

describe.only('add_attributes to elem', function () {

    it('test - same element different attributes', function () {
    let testhtml = document.createElement('div');
    testhtml.setAttribute('class', 'foo');
    let testhtml2 = document.createElement('div');
    //WHY ARE TWO DIFFERENT HTML ELEMENTS DEEP STRICT EQUAL.
    assert.deepStrictEqual(testhtml, testhtml2);

    });
    it('test2 - different elements', function () {
    let testhtml = document.createElement('div');
    let testhtml2 = document.createElement('span');
    //WHY ARE TWO DIFFERENT HTML ELEMENTS DEEP STRICT EQUAL.
    assert.deepStrictEqual(testhtml, testhtml2);

    });
});

【问题讨论】:

    标签: javascript mocha.js assert


    【解决方案1】:

    document.createElement 是“浏览器代码”,所以它不应该是我的单元测试的一部分。

    我不确定 assert.deepStrictEqual 在比较由 document.createElement() 创建的不同对象时是否应该返回 false 但这是一个不好的单元测试,除非你正在测试 document.createElement 本身(而不是你的逻辑)。

    在此处查看两个答案: Javascript Unit Testing - DOM Manipulation

    【讨论】:

      【解决方案2】:

      这里有两个不同的对象,每个对象都有自己的属性。 所以 deepStrictEqual 返回 false。

      nodejs.org 上的文档说:

      “深度”相等意味着子对象的可枚举“自己”属性也通过以下规则递归评估。

      看看https://nodejs.org/api/all.html#assert_assert_deepstrictequal_actual_expected_message

      【讨论】:

      • 是的,但是第一个测试返回 true - 这不是我所期望的......(带有断言的 mocha.js)
      猜你喜欢
      • 2014-09-14
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2019-10-03
      相关资源
      最近更新 更多