我有点像黑客(即编写一些代码并手动测试功能),但现在我想通过添加一些单元测试并遵循 TDD 方法来为我的编码添加更多结构。但我正在努力构建一个验证方法的单元测试。因此,任何见解将不胜感激。
我想测试我的readDirectories方法,但由于它是异步的,我将不得不使用 setTimeout - 但我的测试不断返回错误:
测试.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)
看过磁带上的例子后,我相信我做的一切都是正确的……但话又说回来,我可能只是在做一些愚蠢的事情!如何让我的测试通过???