I'm a bit of a hacker (i.e. write some code and manually test the functionality) but now I want to add a bit more structure to my coding by adding some unit tests and follow a TDD approach. But I'm struggling to build a unit test that validates a method. So any insight would be appreciated.
I want to test my readDirectories method, but as it's asynchronous I'm going to have to use setTimeout - but my test keeps returning an error:
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);
});
console
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)
Having looked at the example on Tape I believe I'm doing everything correctly...but then again I could just be doing something stupid! How do I get my test to pass???