[英]How to check if object property exists with a variable holding the property name?
[英]How to verify that the property name of an object exists?
我正在為我的項目進行開玩笑測試,並且我想驗證博客文章的唯一標識符是否名為id 。
這是我的 mongoose db 博文集:
[
{
title: 'Breaking Ice',
author: 'Mike Vasovsky',
url: 'www.mikey.com',
likes: 14,
id: '5fe21b5931f6ae09d4cc58b6'
}
]
我未完成的部分代碼:
test("verify id property name", async () => {
const response = await api
.get('/api/blogs')
expect(...).toBeDefined("id")
})
如有疑問,請使用 boolean。
expect(response && response.every(blog => blog && blog.id)).toBe(true);
這也有效。 而且,對於那些需要完整片段的人。
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.