Javascript 单元测试应该是;通过;?为什么会失败? 功能渲染库(库存){ //创建一个平面列表 var flatList=''; //对清单进行迭代 对于(变量i=0;i
结果: 失败[应呈现库存项目的平面列表]预期为“布鲁内洛·库奇内利,塔塞尔黑色低帮系带,1000 Brunello Cucinelli,流苏绿色低帮系带,1100 布鲁内洛·库奇内利,原色米色翻毛皮摩卡鞋,950 Brunello Cucinelli,纯橄榄翻毛皮摩卡鞋,1050 Gucci,红色皮革系带运动鞋,800 古奇,黑色皮革鞋带运动鞋,900 但得到了“Gucci,黑色皮革鞋带运动鞋,900” " 我试图正确地添加代码。希望我做得对。我是个编码新手。我的问题是,这个单元测试代码块应该给出“通过的”。我哪里做错了?有人能澄清一下吗。我刚发现问题。Javascript 单元测试应该是;通过;?为什么会失败? 功能渲染库(库存){ //创建一个平面列表 var flatList=''; //对清单进行迭代 对于(变量i=0;i,javascript,unit-testing,tdd,Javascript,Unit Testing,Tdd,结果: 失败[应呈现库存项目的平面列表]预期为“布鲁内洛·库奇内利,塔塞尔黑色低帮系带,1000 Brunello Cucinelli,流苏绿色低帮系带,1100 布鲁内洛·库奇内利,原色米色翻毛皮摩卡鞋,950 Brunello Cucinelli,纯橄榄翻毛皮摩卡鞋,1050 Gucci,红色皮革系带运动鞋,800 古奇,黑色皮革鞋带运动鞋,900 但得到了“Gucci,黑色皮革鞋带运动鞋,900” " 我试图正确地添加代码。希望我做得对。我是个编码新手。我的问题是,这个单元测试代码块应该给
flatList**+=**designerObject.name+”、“+currentShoe.name+”、“+currentShoe.price+”\n'代码>
函数renderInventory(库存)中缺少一个添加项。谢谢大家! 我猜无论函数renderInventory
是什么,它都没有达到您所期望的效果-如果您console.log(actualFlatList)
是否如您所期望的那样?请检查空格/新行/逗号。另外,如果你把结果打印在问题上也会很有帮助。Jaromanda X,是的,你是对的<代码>var actualFlatList=renderInventory(当前库存)代码>未执行此任务。我怎么能解决这个问题?我搞错了,我解决了。仍然“失败”
function renderInventory(inventory) {
//create a flat list
var flatList = '';
//iterate over the inventory
for (var i = 0; i < inventory.length; i++) {
var designerObject = inventory[i];
var shoes = designerObject.shoes;
//iterate over the each shoe in the array
for (var j = 0; j < shoes.length; j++) {
var currentShoe = shoes[j];
//add the designer name, the shoe name, and the shoe price and the new line
flatList = designerObject.name +", " + currentShoe.name +", " + currentShoe.price + '\n';
}
}
//return the flat list
return flatList;
}
//assertion Function
function assertEqual(actual, expected, testName) {
if (actual === expected) {
console.log('passed');
} else {
console.log('FAILED [' + testName + '] Expected "' + expected + '", but got "' + actual + "'");
}
}
//test cases
var currentInventory = [{
name: 'Brunello Cucinelli',
shoes: [
{name: 'tasselled black low-top lace-up', price: 1000},
{name: 'tasselled green low-top lace-up', price: 1100},
{name: 'plain beige suede moccasin', price: 950},
{name: 'plain olive suede moccasin', price: 1050}
]
},
{
name: 'Gucci',
shoes: [
{name: 'red leather laced sneakers', price: 800},
{name: 'black leather laced sneakers', price: 900}
]
}
];
var actualFlatList = renderInventory(currentInventory);
var expectedFlatList = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000\nBrunello Cucinelli, tasselled green low-top lace-up, 1100\nBrunello Cucinelli, plain beige suede moccasin, 950\nBrunello Cucinelli, plain olive suede moccasin, 1050\nGucci, red leather laced sneakers, 800\nGucci, black leather laced sneakers, 900\n';
assertEqual(actualFlatList, expectedFlatList, "should render flat list of inventory items");