Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环对象并检查属性是否存在-Mocha/Chai_Javascript_Loops_Object_Mocha.js_Chai - Fatal编程技术网

Javascript 循环对象并检查属性是否存在-Mocha/Chai

Javascript 循环对象并检查属性是否存在-Mocha/Chai,javascript,loops,object,mocha.js,chai,Javascript,Loops,Object,Mocha.js,Chai,我正在mocha/chai/NodeJs中设置一些单元测试。我正在测试fetchapi调用中返回的响应 我的回答中有9个对象。我想检查每个对象是否包含一个名为“largeImageURL”的属性。但是,我们正在努力寻找最好的方法来做到这一点 到目前为止,我已经在我的对象上使用了.map,找到了largeImageURL,并返回了每个值。并检查其中9个是否存在 这起作用并返回: [ { largeImageURL: 'https://pixabay.com/get/55e3d3444a5ab108

我正在mocha/chai/NodeJs中设置一些单元测试。我正在测试fetchapi调用中返回的
响应

我的回答中有9个对象。我想检查每个对象是否包含一个名为“largeImageURL”的属性。但是,我们正在努力寻找最好的方法来做到这一点

到目前为止,我已经在我的对象上使用了
.map
,找到了
largeImageURL
,并返回了每个值。并检查其中9个是否存在

这起作用并返回:

[ { largeImageURL: 'https://pixabay.com/get/55e3d3444a5ab108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/55e3d3474d57b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/50e6dd454b54b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/54e6dc4a4f50ac14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e7d244435aaa14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/53e6dc434351b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/52e0d2444a51b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e8d0404f57ae14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e8d0434b50a514f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg' } ]
但实际上我想做这样的事情:

expect(res.hits).to.have.key(largeImageURL);
但是我不能让我的
期望
以这种方式工作。这可能吗

以下是我到目前为止所写的内容:

describe('1) Check for succcessful fetech API call', () => {
it('a) Should return an object, with an array count of 9 elements', async () => {
    await fetch('https://pixabay.com/api/?key=MY_API_KEY&q=manhattan&image_type=photo&page=1&per_page=9')
        .then((res) => {
            return res.json()
        })
        .then((res) => {
            console.log(res.hits);
            expect(res).to.be.an('object');
            expect(res.hits).to.have.lengthOf(9);

            const imageURLs = res.hits.map(x => ({largeImageURL: x.largeImageURL}));
            expect(imageURLs).to.have.lengthOf(9);
        })
})
})

顺便说一下,Console.log会产生这种情况。任何想法都将不胜感激

  1) Check for succcessful fetech API call
[ { largeImageURL: 'https://pixabay.com/get/55e3d3444a5ab108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg',
    webformatHeight: 360,
    webformatWidth: 640,
    likes: 646,
    imageWidth: 2560,
    id: 336708,
    user_id: 242387,
    views: 302373,
    comments: 141,
    pageURL: 'https://pixabay.com/photos/manhattan-empire-state-building-336708/',
    imageHeight: 1440,
    webformatURL: 'https://pixabay.com/get/55e3d3444a5ab108f5d084609621317b133fd8e04e50744e762c79d2904fc1_640.jpg',
    type: 'photo',
    previewHeight: 84,
    tags: 'manhattan, empire state building, new york city',
    downloads: 141972,
    user: 'Free-Photos',
    favorites: 775,
    imageSize: 1587009,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2014/05/07/00-10-34-2_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2014/05/03/01/04/manhattan-336708_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/55e3d3474d57b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg',
    webformatHeight: 426,
    webformatWidth: 640,
    likes: 775,
    imageWidth: 3000,
    id: 336475,
    user_id: 242387,
    views: 266538,
    comments: 110,
    pageURL: 'https://pixabay.com/photos/new-york-city-brooklyn-bridge-night-336475/',
    imageHeight: 2000,
    webformatURL: 'https://pixabay.com/get/55e3d3474d57b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_640.jpg',
    type: 'photo',
    previewHeight: 99,
    tags: 'new york city, brooklyn bridge, night',
    downloads: 110902,
    user: 'Free-Photos',
    favorites: 724,
    imageSize: 1230835,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2014/05/07/00-10-34-2_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2014/05/02/23/46/new-york-city-336475_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/50e6dd454b54b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg',
    webformatHeight: 426,
    webformatWidth: 640,
    likes: 443,
    imageWidth: 5184,
    id: 668616,
    user_id: 777390,
    views: 241940,
    comments: 72,
    pageURL: 'https://pixabay.com/photos/new-york-skyline-new-york-city-city-668616/',
    imageHeight: 3456,
    webformatURL: 'https://pixabay.com/get/50e6dd454b54b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_640.jpg',
    type: 'photo',
    previewHeight: 99,
    tags: 'new york, skyline, new york city',
    downloads: 84616,
    user: 'mpewny',
    favorites: 439,
    imageSize: 8661087,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2018/02/04/18-35-12-410_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2015/03/11/12/31/new-york-668616_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/54e6dc4a4f50ac14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg',
    webformatHeight: 426,
    webformatWidth: 640,
    likes: 351,
    imageWidth: 8256,
    id: 2699520,
    user_id: 4591426,
    views: 109012,
    comments: 35,
    pageURL: 'https://pixabay.com/photos/new-york-cityscape-night-city-2699520/',
    imageHeight: 5504,
    webformatURL: 'https://pixabay.com/get/54e6dc4a4f50ac14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_640.jpg',
    type: 'photo',
    previewHeight: 99,
    tags: 'new york, cityscape, night',
    downloads: 54675,
    user: 'igormattio',
    favorites: 332,
    imageSize: 11053173,
    previewWidth: 150,
    userImageURL: '',
    previewURL: 'https://cdn.pixabay.com/photo/2017/08/31/05/36/new-york-2699520_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e7d244435aaa14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg',
    webformatHeight: 426,
    webformatWidth: 640,
    likes: 316,
    imageWidth: 4867,
    id: 1777986,
    user_id: 3628174,
    views: 80039,
    comments: 34,
    pageURL: 'https://pixabay.com/photos/usa-manhattan-contrasts-new-york-1777986/',
    imageHeight: 3246,
    webformatURL: 'https://pixabay.com/get/57e7d244435aaa14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_640.jpg',
    type: 'photo',
    previewHeight: 99,
    tags: 'usa, manhattan, contrasts',
    downloads: 42274,
    user: 'wiggijo',
    favorites: 384,
    imageSize: 5819085,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2016/10/28/09-04-12-659_250x250.jpeg',
    previewURL: 'https://cdn.pixabay.com/photo/2016/10/28/13/09/usa-1777986_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/53e6dc434351b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg',
    webformatHeight: 640,
    webformatWidth: 640,
    likes: 255,
    imageWidth: 2048,
    id: 569093,
    user_id: 364018,
    views: 98250,
    comments: 28,
    pageURL: 'https://pixabay.com/photos/city-manhattan-sunset-town-569093/',
    imageHeight: 2048,
    webformatURL: 'https://pixabay.com/get/53e6dc434351b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_640.jpg',
    type: 'photo',
    previewHeight: 150,
    tags: 'city, manhattan, sunset',
    downloads: 55895,
    user: 'Life-Of-Pix',
    favorites: 370,
    imageSize: 698912,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2014/08/21/23-01-42-554_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2014/12/15/13/57/city-569093_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/52e0d2444a51b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_1280.jpg',
    webformatHeight: 301,
    webformatWidth: 640,
    likes: 263,
    imageWidth: 5183,
    id: 407703,
    user_id: 242387,
    views: 100552,
    comments: 64,
    pageURL: 'https://pixabay.com/photos/manhattan-new-york-city-407703/',
    imageHeight: 2444,
    webformatURL: 'https://pixabay.com/get/52e0d2444a51b108f5d084609621317b133fd8e04e50744e762c79d2904fc1_640.jpg',
    type: 'photo',
    previewHeight: 70,
    tags: 'manhattan, new york city, empire state building',
    downloads: 55127,
    user: 'Free-Photos',
    favorites: 375,
    imageSize: 3543546,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2014/05/07/00-10-34-2_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2014/08/01/15/51/manhattan-407703_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e8d0404f57ae14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg',
    webformatHeight: 425,
    webformatWidth: 640,
    likes: 204,
    imageWidth: 4201,
    id: 1853552,
    user_id: 2286921,
    views: 64835,
    comments: 8,
    pageURL: 'https://pixabay.com/photos/architecture-new-york-city-manhattan-1853552/',
    imageHeight: 2790,
    webformatURL: 'https://pixabay.com/get/57e8d0404f57ae14f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_640.jpg',
    type: 'photo',
    previewHeight: 99,
    tags: 'architecture, new york city, manhattan',
    downloads: 36684,
    user: 'Pexels',
    favorites: 279,
    imageSize: 3935888,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2016/03/26/22-06-36-459_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2016/11/23/15/32/architecture-1853552_150.jpg' },
  { largeImageURL: 'https://pixabay.com/get/57e8d0434b50a514f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_1280.jpg',
    webformatHeight: 360,
    webformatWidth: 640,
    likes: 145,
    imageWidth: 5000,
    id: 1850129,
    user_id: 2286921,
    views: 59983,
    comments: 7,
    pageURL: 'https://pixabay.com/photos/architecture-new-york-city-manhattan-1850129/',
    imageHeight: 2813,
    webformatURL: 'https://pixabay.com/get/57e8d0434b50a514f6da8c7dda793e781039dee3564c704c722c78d6914ac75c_640.jpg',
    type: 'photo',
    previewHeight: 84,
    tags: 'architecture, new york city, manhattan',
    downloads: 34065,
    user: 'Pexels',
    favorites: 258,
    imageSize: 3275708,
    previewWidth: 150,
    userImageURL: 'https://cdn.pixabay.com/user/2016/03/26/22-06-36-459_250x250.jpg',
    previewURL: 'https://cdn.pixabay.com/photo/2016/11/22/19/17/architecture-1850129_150.jpg' } ]
    ✓ a) Should return an object, with an array count of 9 elements (196ms)
你可以使用这个插件

在下面的代码片段中,我将chaiEach包含为用于浏览器的函数

chai.use(chaiEach);
expect=chai.expect;
test=函数(testName,应为){
试一试{
expect(expected).each.have.property('foo');
log(testName+“已成功”);
}捕获(e){
log(testName+“失败,带“+e”);
}
};
测试(“标称情况”,[{foo:'bar'},{foo:'buzz'}]);
测试(“错误案例”[{foo:'bar'},{bar:0},{foo:'buzz'}])

chaiEach=功能(chai,utils){
constPropertyNames=Object.getOwnPropertyNames(chai.Assertion.prototype);
const propertyDescs={};
for(属性名称的常量名称){
propertyDescs[name]=Object.getOwnPropertyDescriptor(chai.Assertion.prototype,name);
}
const methodNames=propertyNames.filter(name=>{
返回名称!=“assert”&&typeof propertyDescs[name]。值==“function”;
});
chai.Assertion.addProperty('each',函数(){
const each=utils.flag(此“each”)| | 0;
utils.标志(此“每个”,每个+1);
});
methodNames.forEach(methodName=>{
chai.Assertion.overwriteMethod(方法名,函数(_super){
返回函数assertEach(){
if(utils.flag(此为“每个”)){
const each=utils.flag(此“each”);
utils.标志(此“每个”,每个-1);
var obj=这个;
新的chai.Assertion(obj.is.an.instanceOf(Array);
对象映射(项=>{
const that=克隆插入(this);
utils.标志(即“对象”项);
_超级。应用(即,参数);
返回utils.flag(即“object”);
});
}否则{
_super.apply(这个,参数);
}
};
});
});
功能克隆插入(原件){
const copy=Object.create(原始);
utils.transferFlags(原件、副本、虚假);
返回副本;
}

};可以使用chai
[{“value”:4},{“value”:11},{“value”:15}].should.each.have.property('value')这不起作用,但谢谢你。。。