我在这里使用断言测试创建了一个函数:
function renderInventory(arr) {
var str = '';
for(var i = 0; i < arr.length; i++){
for(var j = 0; j < arr[i].shoes.length; j++){
str += arr[i].name + ', ' + arr[i].shoes[j].name + ', ' + arr[i].shoes[j].price + '\n';
}
}
return str;
}
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 actual = renderInventory(currentInventory);
var expected = "Brunello Cucinelli, tasselled black low-top lace-up, 1000" + '\n' +
"Brunello Cucinelli, tasselled green low-top lace-up, 1100" + '\n' +
"Brunello Cucinelli, plain beige suede moccasin, 950" + '\n' +
"Brunello Cucinelli, plain olive suede moccasin, 1050" + '\n' +
"Gucci, red leather laced sneakers, 800" + '\n' +
"Gucci, black leather laced sneakers, 900";
function assertrenderInventory(actual, expected, testName){
if(actual === expected){
console.log('passed');
}else{
console.log('FAILED [' + testName + '] Expected "' + actual + '" to be "' + expected + '"');
}
}
assertrenderInventory(actual, expected, 'testing renderInventory');
基本上它的作用是通过遍历数组,将designer name
和shoe name
与price
一起放在一行中。但由于某些原因,无论我如何尝试进行测试,它仍然失败,并且摩卡测试中存在“分裂”错误。
这是摩卡测试:
describe('render inventory', function() {
it('renders_first_line_correctly', function() {
var inventory = [
{
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 actual = renderInventory(inventory).split('\n');
var expected = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000';
expect(actual[0]).toBe(expected);
});
it('renders_second_line_correctly', function() {
var inventory = [
{
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 actual = renderInventory(inventory).split('\n');
var expected = 'Gucci, black leather laced sneakers, 900';
expect(actual.pop()).toBe(expected);
});
});
我知道如何在这里通过测试?
您可以在此处查看我的代码:https://jsbin.com/zeviponeda/edit?html,js,console
答案 0 :(得分:0)
你的renderInventory也会将“\ n”附加到结束行,但你的期望不会以'\ n'结尾;实际预期应为
var expected = "Brunello Cucinelli, tasselled black low-top lace-up,
1000" + '\n' +
"Brunello Cucinelli, tasselled green low-top lace-up, 1100" + '\n' +
"Brunello Cucinelli, plain beige suede moccasin, 950" + '\n' +
"Brunello Cucinelli, plain olive suede moccasin, 1050" + '\n' +
"Gucci, red leather laced sneakers, 800" + '\n' +
"Gucci, black leather laced sneakers, 900" + '\n';
出于同样的原因,actual.pop()导致空字符串