[英]How to verify that the property name of an object exists?
I am making jest tests for my project and I want to verify that the unique identifier of the blog posts is named id .我正在为我的项目进行开玩笑测试,并且我想验证博客文章的唯一标识符是否名为id 。
This is my mongoose db blog posts collection:这是我的 mongoose db 博文集:
[
{
title: 'Breaking Ice',
author: 'Mike Vasovsky',
url: 'www.mikey.com',
likes: 14,
id: '5fe21b5931f6ae09d4cc58b6'
}
]
My not finished part of the code:我未完成的部分代码:
test("verify id property name", async () => {
const response = await api
.get('/api/blogs')
expect(...).toBeDefined("id")
})
When in doubt, use a boolean.如有疑问,请使用 boolean。
expect(response && response.every(blog => blog && blog.id)).toBe(true);
This works too.这也有效。 And, for those who need a complete snippet.而且,对于那些需要完整片段的人。
test('verify id property name', async () => {
const response = await api.get('/api/blogs');
response.body.forEach(blog => {
expect(blog.id).toBeDefined();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.