Javascript Jasmine-使用forEach测试对象数组中的键/值对是否已定义
我有一个名为AllFeed的对象数组。每个对象都有一个“name”键,该键有一个名为“url”的关联值。我正试图用Jasmine编写一个测试,它在数组中循环并测试每个对象,以确保没有“url”值是“未定义的”。我的代码如下。我尝试了几次语法/逻辑的迭代,但都没有成功Javascript Jasmine-使用forEach测试对象数组中的键/值对是否已定义,javascript,arrays,function,foreach,jasmine,Javascript,Arrays,Function,Foreach,Jasmine,我有一个名为AllFeed的对象数组。每个对象都有一个“name”键,该键有一个名为“url”的关联值。我正试图用Jasmine编写一个测试,它在数组中循环并测试每个对象,以确保没有“url”值是“未定义的”。我的代码如下。我尝试了几次语法/逻辑的迭代,但都没有成功 it('each object in allFeeds array should have a URL value associated with each key', function() { allF
it('each object in allFeeds array should have a URL value associated with each key', function() {
allFeeds.forEach(function (arrayItem) {
var x = arrayItem.url
return x;
});
expect(x).not.toBeUndefined();
});
看起来你把支票放错位置了
it('each object in allFeeds array should have a URL value associated with each key', function() {
allFeeds.forEach(function (arrayItem) {
var x = arrayItem.url
// Check 'x' here
expect(x).not.toBeUndefined();
});
// Worng position - 'x' exists only in the loop
// expect(x).not.toBeUndefined();
});
看起来你把支票放错位置了
it('each object in allFeeds array should have a URL value associated with each key', function() {
allFeeds.forEach(function (arrayItem) {
var x = arrayItem.url
// Check 'x' here
expect(x).not.toBeUndefined();
});
// Worng position - 'x' exists only in the loop
// expect(x).not.toBeUndefined();
});