茉莉javascript:如何检查响应包含的东西?



如何检查包含postText与wuihduwiwith和userId: '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介绍页面,toContain"用于查找数组中的项"和也适用于寻找子字符串。他们没有提到使用toContain来查找对象中的对象。

如果响应的结构始终相同/userIdpostText字段始终在同一位置,则只需执行:

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();
});
});

最新更新