2016-08-06 9 views
1

私はちょっとしたハッカーです(コードを書いて手動で機能をテストする)が、今では単体テストを追加してTDDの手法に従うことで、コーディングにもう少し構造を追加したいと考えています。しかし、私は方法を検証する単体テストを作るのに苦労しています。だから、どんな洞察にも感謝します。テストメソッドの呼び出し

私は私のreadDirectoriesメソッドをテストしたいが、それは非同期だと私はのsetTimeoutを使用する必要がありますするつもりだ - しかし、私のテストはエラーを返す保つ:

test.js

test('The readDirectories method', (t) => { 
    const xyz = new abc('/foo/bar/', '/foo/baz/'); 
    const expected = ['/foo/bar/Download', '/foo/bar/HardDrive' ]; 

    xyz.readDirectories(xyz.source) 

    setTimeout(() => { 
    let actual = xyz.directories; 
    t.equal(actual, expected, 'should produce an array of subdirectories'); 
    }, 10); 

}); 

コンソール

operator: equal 
expected: |- 
    [ '/foo/bar/Download', '/foo/bar/HardDrive' ] 
actual: |- 
    [ '/foo/bar/Download', '/foo/bar/HardDrive' ] 
at: Timeout.setTimeout (/home/user/Documents/app/tests/testModel.js:33:7) 

Tapeの例を見て、私はすべてを正しくやっていると信じています...しかし、もう一度私はちょっとばかげたことをすることができます!私のテストをパスするにはどうすればいいですか?

答えて

1

.equalテストのため、テストに失敗していました。 .equalテストではmy結果が両方の配列であるのに対し、.deepEqualでは2つの配列が同じ構造とネストされた値を持つことをテストします。

Tape ウェブサイトから:

t.equal()

Assert that actual === expected with an optional description msg.

t.deepEqual()

Assert that actual and expected have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.

関連する問題