Should.js在一个属性上链接多个断言
我有这样一个对象:
var obj = { "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51", "type": "candy" }
我想写一个testing,首先检查对象的属性“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])
而这(实际上不会使语法意义 – 因为它将适用于父对象而不是值)
obj.should.have.property('uuid').and.be.length(36)
哪个失败:
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')
那么链接对象属性的断言的正确方法是什么?
我认为这可能是更好的select:
var session = { "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51", "type": "candy" }; session.should.have.property('uuid').with.a.lengthOf(36);
或者如果你想要select这个should
两次,但我不认为这是一个正确的方式(下面解释)。
var session = { "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51", "type": "candy" }; session.should.have.property('uuid').which.obj.should.have.length(36);
你可以看到他们在这里工作:
https://jsfiddle.net/Lz2zsoks/
.an
, .of
, .a
, .and
,.be,.have, .with
,.is,。这些只不过是无用的链。
更新
作为对@denbardadym的回应,我会试着解释为什么你不应该使用两次:
- 你不会在自然语言中使用它两次,所以最好不要在testing中使用它
- Should.js不打算以这种方式使用。 在库文档中找不到这种用法的任何示例。
第一个陈述失败,因为你打电话, .should
两次 – 你第二次断言断言,应该是:
obj.should.have.property('uuid').which.have.length(36)
(错误消息从字面上说,这个Assertion {...}
没有属性长度)
第二个陈述并不适合我:
obj.should.have.property('uuid').and.be.length(36)
(你的错误信息看起来不像你断言失败)
最后的声明 – 没有.equals
断言 – 它应该是.equal
。 这是因为"0320a79a-920d-11e5-9b7a-057d4ca344ba" !== "60afc3fa-920d-11e5-bd17-b9db323e7d51"
希望能帮助到你。