2016-08-06 113 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測試我的結果是兩個陣列而.deepEqual測試,兩個陣列具有相同的結構和嵌套的值。

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.