vendredi 25 septembre 2015

Return value for asynchronous callback (in unit test)

I am attempting to do jasmine-node unit testing on my node.js project. I need to get an array of file paths and then pass that array to the unit test. However, since this is asynchronous, I have to use a callback, and I can't return the array. I can't figure out how to send the retrieved array to the unit test.

Here's my code:

module.exports = {
    results: [],
    walk: function(){
        var nodeDir = require( "node-dir" );
        nodeDir.files( __dirname + "/public/img", function( err, files ){
            if ( err ){ throw err; }    
            getData( files );
        } );  
    },
    getData: function( files ){
        console.log( files );
        results = files;
    }
};

and here's my unit test:

var gallery = require( "./server" );

describe( "Gallery", function() {
    var picArray = [
        "/gallery/public/img/dir1/lizard-248705_1280.jpg",
        "/gallery/public/img/dir1/loch-ness-151851_1280.png",
        "/gallery/public/img/dir1/sand-lizard-63185_1280.jpg",
        "/gallery/public/img/dir1/stegosaurus-24752_1280.png"];

    it( "walks the images directory", function(){
        expect(gallery.walk()).toEqual(picArray);
    });
});

Aucun commentaire:

Enregistrer un commentaire