【问题标题】:How does one do a deepEqual assertion with should.js?如何使用 should.js 进行 deepEqual 断言?
【发布时间】:2013-10-08 06:13:01
【问题描述】:

我尝试使用 should.js(最新版本)进行 deepEqual 断言,但没有成功。我可以使用equal,但不能使用deepEqual。事实上我看到没有deepEqual 方法。

这是我尝试过的:

> require('should')
{...}
> > var x = Number(8)
undefined
> x.should.equal(8)
{ obj: 8 }
> x.should.equal(9)
AssertionError: expected 8 to equal 9
at ....
> x.should.deepEqual(8)
TypeError: Object #<Object> has no method 'deepEqual'

很公平。现在查看should,我发现它是一个吸气剂:

> Object.getOwnPropertyDescriptor(Object.prototype, 'should')
{ get: [Function],
  set: [Function],
  enumerable: false,
  configurable: true }

既然它是一个吸气剂,我如何检查它的键?这几乎可以工作:

> Object.keys(Object.prototype.should)
[ 'obj' ]

然后我明白了

> Object.getOwnPropertyDescriptor(should.obj)
{ value: undefined,
  writable: false,
  enumerable: false,
  configurable: false }

所以我还是卡在了这一点上。我只是想看看should 会发生什么。

做了read the docs 并且它说should.js 从字面上扩展了节点的断言模块,但节点的断言确实允许deepEqual

> assert = require('assert')
> assert.deepEqual
[Function: deepEqual]

应该文档甚至根本没有提到deepEqual,这真的让我很困惑。为了让事情变得更加混乱,当我在节点 REPL 上输入 should 时,我确实看到了 deepEqual。但据我所知,它隐藏在 ok 元素中。

TL;DR:我如何从should 调用assertEqual 或其等效项?

【问题讨论】:

    标签: javascript node.js should.js deepequals


    【解决方案1】:

    我认为你应该(双关语)使用eql 方法。

    https://github.com/visionmedia/should.js/#eql

    ({ foo: 'bar' }).should.eql({ foo: 'bar' })
    

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2015-05-22
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多