2016-08-06 46 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.