Jasmine javascript : 如何检查响应是否包含某些内容?
Jasmine javascript : How to check response contain something?
如何检查响应是否包含带有 wuihduwih 和用户 ID 的 postText:'60f469cf784379051298e96d'
这是回复:
({ id: '612bf0792ca01806398da2d6', data: Object({ createdOn: '2021-08-29T20:39:21.445Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'wuihduwih', postImages: [ ], pluginInstance: Object({ pluginInstanceId: '1627334094776-047554258642281355', pluginInstanceTitle: 'communityFeedPlugin' }), isPublic: false, _buildfire: Object({ index: Object({ array1: [ Object({ string1: 'userId_60f469cf784379051298e96d' }), Object({ string1: 'displayName_nadia' }), Object({ string1: 'pluginTitle_communityfeedplugin' }), Object({ string1: 'isPublic_0' }) ] }) }) }), tag: 'posts' })
尝试了以下但失败了:
Posts.addPost({postText :'wuihduwih'},(err,resp) => {
expect(resp).toContain(jasmine.objectContaining({
postText :'wuihduwih'
}));
done();
}); ```
基于 Jasmine introductory page,toContain
“适用于查找数组中的项目”和“也适用于查找子字符串”。他们没有提到使用 toContain
在对象中查找对象。
如果响应的结构总是 same/the 字段 userId
和 postText
总是在同一个地方,你可以这样做:
expect(resp.data.postText).toEqual('wuihduwih');
和
expect(resp.data.hasOwnProperty('userId')).toBe(true);
或者,如果 userId
永远不会改变:
expect(resp.data.hasOwnProperty('userId')).toEqual('60f469cf784379051298e96d');
基于相同的介绍页面,您也可以像您尝试的那样使用 objectContaining
,但结合 toEqual
而不是 toContain
:
expect(resp.data).toEqual(jasmine.objectContaining({
postText: 'wuihduwih',
userId: '60f469cf784379051298e96d'
}));
请注意,我传入 resp.data
,而不仅仅是 resp
。引用的页面没有说明它是否适用于嵌套对象。
如果我想验证数组响应的结果怎么办!
({ id: '612d56952ca01806398dac1f', lastUpdated: '2021-08-30T22:07:17.099Z', userToken: 'public', data: Object({ createdOn: '2021-08-30T22:07:16.916Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'post text sample', postImages: 'image test', pluginInstance: Object({ pluginInstanceId: '1627334094776-047554258642281355', pluginInstanceTitle: 'communityFeedPlugin' }), isPublic: true, _buildfire: Object({ index: Object({ array1: [ Object({ string1: 'userId_60f469cf784379051298e96d' }), Object({ string1: 'displayName_nadia' }), Object({ string1: 'pluginTitle_communityfeedplugin' }), Object({ string1: 'isPublic_1' }) ] }) }) }) }), Object({ id: '612d564dfabce50627bdb5db', lastUpdated: '2021-08-30T22:06:05.699Z', userToken: 'public', data: Object({ createdOn: '2021-08-30T22:06:05.500Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'post text sample', postImages: 'image test', pluginInstance: Object ...
尝试了以下但没有奏效:
it('Get all posts' , function(done){
Posts.getPosts('publicPosts' ,(err,resp) =>{
expect(resp).toContain(jasmine.objectContaining({
id: '612d50bafabce50627bdb538',
postText:'post text sample',
userId: '60f469cf784379051298e96d',
isPublic: true
}));
done();
});
});
如何检查响应是否包含带有 wuihduwih 和用户 ID 的 postText:'60f469cf784379051298e96d'
这是回复:
({ id: '612bf0792ca01806398da2d6', data: Object({ createdOn: '2021-08-29T20:39:21.445Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'wuihduwih', postImages: [ ], pluginInstance: Object({ pluginInstanceId: '1627334094776-047554258642281355', pluginInstanceTitle: 'communityFeedPlugin' }), isPublic: false, _buildfire: Object({ index: Object({ array1: [ Object({ string1: 'userId_60f469cf784379051298e96d' }), Object({ string1: 'displayName_nadia' }), Object({ string1: 'pluginTitle_communityfeedplugin' }), Object({ string1: 'isPublic_0' }) ] }) }) }), tag: 'posts' })
尝试了以下但失败了:
Posts.addPost({postText :'wuihduwih'},(err,resp) => {
expect(resp).toContain(jasmine.objectContaining({
postText :'wuihduwih'
}));
done();
}); ```
基于 Jasmine introductory page,toContain
“适用于查找数组中的项目”和“也适用于查找子字符串”。他们没有提到使用 toContain
在对象中查找对象。
如果响应的结构总是 same/the 字段 userId
和 postText
总是在同一个地方,你可以这样做:
expect(resp.data.postText).toEqual('wuihduwih');
和
expect(resp.data.hasOwnProperty('userId')).toBe(true);
或者,如果 userId
永远不会改变:
expect(resp.data.hasOwnProperty('userId')).toEqual('60f469cf784379051298e96d');
基于相同的介绍页面,您也可以像您尝试的那样使用 objectContaining
,但结合 toEqual
而不是 toContain
:
expect(resp.data).toEqual(jasmine.objectContaining({
postText: 'wuihduwih',
userId: '60f469cf784379051298e96d'
}));
请注意,我传入 resp.data
,而不仅仅是 resp
。引用的页面没有说明它是否适用于嵌套对象。
如果我想验证数组响应的结果怎么办!
({ id: '612d56952ca01806398dac1f', lastUpdated: '2021-08-30T22:07:17.099Z', userToken: 'public', data: Object({ createdOn: '2021-08-30T22:07:16.916Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'post text sample', postImages: 'image test', pluginInstance: Object({ pluginInstanceId: '1627334094776-047554258642281355', pluginInstanceTitle: 'communityFeedPlugin' }), isPublic: true, _buildfire: Object({ index: Object({ array1: [ Object({ string1: 'userId_60f469cf784379051298e96d' }), Object({ string1: 'displayName_nadia' }), Object({ string1: 'pluginTitle_communityfeedplugin' }), Object({ string1: 'isPublic_1' }) ] }) }) }) }), Object({ id: '612d564dfabce50627bdb5db', lastUpdated: '2021-08-30T22:06:05.699Z', userToken: 'public', data: Object({ createdOn: '2021-08-30T22:06:05.500Z', lastUpdatedBy: null, userId: '60f469cf784379051298e96d', displayName: 'Nadia', postText: 'post text sample', postImages: 'image test', pluginInstance: Object ...
尝试了以下但没有奏效:
it('Get all posts' , function(done){
Posts.getPosts('publicPosts' ,(err,resp) =>{
expect(resp).toContain(jasmine.objectContaining({
id: '612d50bafabce50627bdb538',
postText:'post text sample',
userId: '60f469cf784379051298e96d',
isPublic: true
}));
done();
});
});