admin管理员组文章数量:1287247
I'm trying to test a function that returns a promise using the chai-as-promised library. The result in my promise is an object with nested properties. Is it possible to test a deeply nested property's value.
E.g.
function myFunc() {
return new Promise((resolve, reject) => {
const data = {
thing: {
foo: 'bar',
baz: 'lah'
}
}
resolve(data)
})
}
How can I test that the foo
property equals "bar" without checking the entire object? I've tried something like this:
expect(myFunc()).to.eventually.have.property('thing.foo', 'bar')
But no luck!
I'm trying to test a function that returns a promise using the chai-as-promised library. The result in my promise is an object with nested properties. Is it possible to test a deeply nested property's value.
E.g.
function myFunc() {
return new Promise((resolve, reject) => {
const data = {
thing: {
foo: 'bar',
baz: 'lah'
}
}
resolve(data)
})
}
How can I test that the foo
property equals "bar" without checking the entire object? I've tried something like this:
expect(myFunc()).to.eventually.have.property('thing.foo', 'bar')
But no luck!
Share Improve this question edited Jun 6, 2016 at 9:46 harryg asked Jun 6, 2016 at 9:38 harrygharryg 24.1k47 gold badges134 silver badges208 bronze badges1 Answer
Reset to default 12Using deep property lookup should work. Just add the deep
keyword before property
.
expect(myFunc()).to.eventually.have.deep.property('thing.foo', 'bar')
If you prefer the verbose way, you should also be able to do stuff like:
expect(myFunc())
.to.eventually.have.property('thing')
.that.has.property('foo')
.that.is.equal.to('bar');
本文标签: javascriptTesting value of nested property in chaiaspromised and mochaStack Overflow
版权声明:本文标题:javascript - Testing value of nested property in chai-as-promised and mocha - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741285218a2370231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论