2016-10-15 4 views
0

オブジェクトリストとidを idと指定した場合、同じリストが返されますが、対応するオブジェクトはアクティブとマークされます オブジェクトはアクティブではありません)。チャイ関連のエラーメッセージ:「AssertionError:期待値が深く等しいと期待されています」

const list = [ 
    { id: 1, active: false }, 
    { id: 2, active: false }, 
    { id: 3, active: true }, 
    { id: 4, active: false } 
]; 

function markActive(list, value) { 
    list.forEach((id) => { 
    if (id.active = (id.id === value)) { 
     return true; 
    } else { 
     return false; 
    } 
    }); 
} 

markActive(list, 2); 
console.log(list) 

戻り値:

[ { id: 1, active: false }, 
    { id: 2, active: false }, 
    { id: 3, active: false }, 
    { id: 4, active: true } ] 

それは私が "NPMの実行[ファイル名]" を実行したときを除いて、魔法のように働いている私は、エラーメッセージが出ます:

Running Tests for [filename]. 
------------ 
[ { id: 1, active: false }, 
    { id: 2, active: false }, 
    { id: 3, active: false }, 
    { id: 4, active: true } ] 


    markActive 
    1) Case 1 (Given Sample) 
    2) Case 2 (String IDs) 


    0 passing (16ms) 
    2 failing 

    1) markActive Case 1 (Given Sample): 
    AssertionError: expected undefined to deeply equal [ { id: 1,   
    active: false }, 
    { id: 2, active: true }, 
    { id: 3, active: false }, 
    { id: 4, active: false } ] 
    at Function.assert.deepEqual    
    (node_modules/chai/lib/chai/interface/assert.js:216:32) 
     at Context.it (tests/test_02.js:23:12) 

    2) markActive Case 2 (String IDs): 
    AssertionError: expected undefined to deeply equal [ { id: '1',  
    active: false }, 
    { id: '2', active: true }, 
    { id: '3', active: false }, 
    { id: '4', active: false } ] 
    at Function.assert.deepEqual 
    (node_modules/chai/lib/chai/interface/assert.js:216:32) 
    at Context.it (tests/test_02.js:40:12) 

任意のアイデアをどのようなI間違ってる?ここでテストをセットアップするコードがあります:

const chai = require("chai"); 
const sinon = require("sinon"); 
const assert = chai.assert; 

const markActive = require("../answers/02.js"); 

describe("markActive",() => { 

    it("Case 1 (Given Sample)",() => { 
    var list = [ 
     { id: 1, active: false }, 
     { id: 2, active: false }, 
     { id: 3, active: true }, 
     { id: 4, active: false } 
     ]; 
    var newList = markActive(list, 2); 
    var targetList = [ 
     { id: 1, active: false }, 
     { id: 2, active: true }, 
     { id: 3, active: false }, 
     { id: 4, active: false } 
     ]; 
    assert.deepEqual(newList, targetList); 
    }); 

    it("Case 2 (String IDs)",() => { 
    var list = [ 
     { id: "1", active: false }, 
     { id: "2", active: false }, 
     { id: "3", active: true }, 
     { id: "4", active: false } 
     ]; 
    var newList = markActive(list, "2"); 
    var targetList = [ 
     { id: "1", active: false }, 
     { id: "2", active: true }, 
     { id: "3", active: false }, 
     { id: "4", active: false } 
     ]; 
    assert.deepEqual(newList, targetList); 
    }); 

}); 
+0

質問を編集し、失敗したテストのコードを追加してください。 – Soviut

+0

私はテストのセットアップに使用したコードですか? –

答えて

1

あなたの関数は何も返していないので、あなたは結果に設定しようとするすべての変数がundefinedとして設定されます。

これを修正するには、機能の最後にreturnステートメントを追加するだけです。

function markActive(list, value) { 
    list.forEach((id) => { 
    if (id.active = (id.id === value)) { 
     return true; 
    } else { 
     return false; 
    } 
    }); 

    return list; // return the updated list 
} 

注:それは配列が参照されているので、あなたはその場で値を変更していることを言及する価値があります。このため、戻り値を記録していなくても、関数の外で定義した配列には更新された結果が残っています。同じリストでmarkActive()関数を何度も実行すると、意図しない副作用が生じる可能性があります。新しいリストを返すには、Javascriptで配列をコピーして深くコピーする方法を調べてください。

+0

ARGH!どのような愚かな間違い。それでおしまい。ありがとう! –

関連する問題