【发布时间】:2020-04-09 00:37:24
【问题描述】:
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");
结果: 失败 [应呈现库存物品的平面列表] 预期“Brunello Cucinelli,流苏黑色低帮系带,1000 Brunello Cucinelli,流苏绿色低帮系带,1100 Brunello Cucinelli,纯米色绒面革莫卡辛鞋,950 Brunello Cucinelli,纯橄榄色绒面革莫卡辛鞋,1050 Gucci,红色皮革系带运动鞋,800 Gucci,黑色皮革系带运动鞋,900 ”,但得到了“Gucci,黑色皮革系带运动鞋,900 '
我尝试正确添加我的代码。希望我做对了。我是编码新手。我的问题是,这个单元测试代码块应该给出'passed'。我哪里做错了?有人可以澄清一下吗?
【问题讨论】:
-
我猜无论
renderInventory的功能是什么,它都没有达到您的预期——如果您console.log(actualFlatList)是您预期的那样吗? -
检查空格/换行符/逗号。此外,如果您在问题中打印结果,这将非常有帮助。
-
Jaromanda X,是的,你是对的。
var actualFlatList = renderInventory(currentInventory);没有做这项工作。我该如何解决这个问题? -
空格弄错了,我修好了。仍然“失败”
标签: javascript unit-testing tdd