Jonathan
Jonathan

Reputation: 986

Jasmine Node testing child processes

Just wrote a module that grabs the current IOStat of a production box to determine if it is being over worked.

Interesting problem - there isn't really a way to testing a callback function with Jasmine; well, at least one that I have found.

Spies were the first thing that came to my mind, but since it actually calls the function directly, that isn't possible. Of course, I could always define this function as a var and do a spy.

proc.exec('iostat -c | tail -n2', function(err, out, s){

    if(err) throw err;

    misc();

}

The only reason why I would want to test this - is because I'm developing on a windows machine that doesn't have all of these commands available, so I would like to intercept it and throw in the expected result.

Upvotes: 2

Views: 1285

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 111062

You have to spy on proc.exec and get the callback function out of the spy.mostRecentCall.args array and call at then by your self:

spyOn(proc, 'exec')
//run your code
proc.exec.mostRecentCall.args[1](true)

As this isn't very convenient you can use sinon, where you can create a stub that call the callback automatically with stub.callsArgWith(index, args).

sinon.stub(proc, 'exec').callsArgWith(1, true);
//run your code

Upvotes: 1

Related Questions