Javascript js是否应该在单个属性上链接多个断言

Javascript js是否应该在单个属性上链接多个断言,javascript,node.js,mocha.js,should.js,Javascript,Node.js,Mocha.js,Should.js,我有一个这样的物体: var obj = { "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51", "type": "candy" } 我想编写一个测试,首先检查对象是否具有属性“uuid”,然后检查“uuid”是否为特定长度(36个字符) 试着这样不行 obj.should.have.property('uuid').which.should.have.length(36) 它失败于: Uncaught AssertionError

我有一个这样的物体:

var obj = {
    "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51",
    "type": "candy"
}
我想编写一个测试,首先检查对象是否具有属性“uuid”,然后检查“uuid”是否为特定长度(36个字符)

试着这样不行

obj.should.have.property('uuid').which.should.have.length(36)
它失败于:

Uncaught AssertionError: expected Assertion {
  obj: '60afc3fa-920d-11e5-bd17-b9db323e7d51',
  params: { operator: 'to have property \'uuid\'' },
  negate: false } to have property 'length' of 36 (got [Function])
Uncaught TypeError: usergridResponse.entity.should.have.property(...).which.should.be.equal.to is not a function
这(实际上在语法上没有任何意义——因为它将应用于父对象而不是值)

它失败于:

Uncaught AssertionError: expected Assertion {
  obj: '60afc3fa-920d-11e5-bd17-b9db323e7d51',
  params: { operator: 'to have property \'uuid\'' },
  negate: false } to have property 'length' of 36 (got [Function])
Uncaught TypeError: usergridResponse.entity.should.have.property(...).which.should.be.equal.to is not a function
即使这样也不行:

obj.should.have.property('uuid').which.equals('60afc3fa-920d-11e5-bd17-b9db323e7d51')

那么,链接对象属性断言的正确方法是什么呢?

第一条语句失败,因为您调用了
。如果在断言上断言两次,应该是:

obj.should.have.property('uuid').which.have.length(36)
(错误消息字面上是sai,
断言{…}
没有属性长度)

第二句话不适合我:

obj.should.have.property('uuid').and.be.length(36)
(您的错误消息看起来不是断言失败)

最后一条语句-没有
.equals
断言-应该是
.equal
。这对我来说是理所当然的,因为“0320a79a-920d-11e5-9b7a-057d4ca344ba”!=“60afc3fa-920d-11e5-bd17-b9db323e7d51”


希望能有帮助

我认为这可能是更好的选择:

var session = {
    "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51",
    "type": "candy"
};

session.should.have.property('uuid').with.a.lengthOf(36);
或者,如果你想选择
应该
两次,但我认为这不是一个合适的方法(下面解释)

您可以看到他们在这里工作:

.an
.of
.a
.be
。have
。with
。is
。这些
仅仅是不起任何作用的链子

更新

作为对@denbardadym的回应,我将尝试解释为什么不应该使用
should
两次:

  • 在自然语言中不会使用它两次,所以最好不要在测试中使用它
  • Should.js不打算以这种方式使用。在库文档中找不到这种用法的任何示例

我想我正在进步。Uuid不匹配是我的部件上的复制失败(ok=)。对于这样的问题,我建议你使用gitter chat for Pull.js repo,我通常回答得更快。这不是建议使用的正确方式。应该两次-最好解释一下为什么它是错误的。好的,已经解释过了。但不给出答案就写答案也是不对的;)另外,obj的长度不应该是36,obj.uuid应该是36。为了清晰起见,我最后使用了:
obj.should.be.an.Object.and.have.property('uuid')。使用.a.lengthOf(36)
继续帮助:obj.should.be.an.Object没有断言任何内容,您需要使用该函数(我认为文档是错误的,如果您不使用obj.should.be.an.Object()它将不工作)检查此小提琴: