我有点黑客的(即写一些代码和手动测试功能),但现在我想添加一些单元测试,更多的结构添加到我的编码,并按照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上的例子,我相信我做的一切都是正确的......但后来我可能只是在做一些愚蠢的事!我如何让我的测试通过?