【发布时间】:2016-11-01 20:36:12
【问题描述】:
我有点像黑客(即编写一些代码并手动测试功能),但现在我想通过添加一些单元测试并遵循 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 上的示例后,我相信我所做的一切都是正确的……但是我可能只是在做一些愚蠢的事情!如何让我的测试通过???
【问题讨论】:
标签: node.js node.js-tape