2017-09-04 8 views
0

を渡していません。モカテストは、私がアサートテストとここに機能を作成し

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'); 

は、基本的にこれが何をするか、それが配列を反復処理して、すべて1行にpriceだとdesigner nameshoe nameを得るということです。しかし何らかの理由でテストをやろうとしても失敗してしまい、モカテストで「分割」エラーが発生します。私はここでテストに合格するために何をすべきか

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

you'r renderInventoryは、エンドラインに「\ n」を追加しますが、あなたは「\ n」がで終わっていないと予想します。実際、同じ理由actual.popため

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'; 

()空の文字列で結果でなければならないと予想

関連する問題