dimanche 15 février 2015

Unit testing with Mocha+PhantomJS async code

I'm new to unit testing, so please forrgive me if my question could be silly. I wrote an unit test using Mocha with PhantomJS and Chai as assertion library. The code that I want to test is the following function:



function speakingNotification(audioStream){
var options = {};
var speechEvents = hark(audioStream, options);

speechEvents.on('speaking', function() {
return 'speaking';
});

speechEvents.on('stopped_speaking', function() {
return 'stopped_speaking';
});
}


As you can see it takes an audioStream parameter as input and then use a librabry called hark.js http://ift.tt/1zowatl for detecting speaking events. The function should return if the user is speaking or not.


So I wrote the following unit test:



describe('Testing speaking notification', function () {
describe('Sender', function(){

var audio = document.createElement('audio');
audio.src = 'data:audio/mp3;base64,//OkVA...'; //audio file with sound

var noAudio = document.createElement('audio');
noAudio.src = 'data:audio/mp3;base64,...'; //audio file with no sound

it('should have a function named "speakingNotification"', function() {
expect(speakingNotification).to.be.a('function');
});

it('speaking event', function () {
var a = speakingNotification(audio);
this.timeout( 10000 );
expect(a).to.equal('speaking');
});

it('stoppedSpeaking event', function () {
var a = speakingNotification(noAudio);
this.timeout( 10000 );
expect(a).to.equal('stopped_speaking');
});

});
});


The test fails and shows:



AssertionError: expected undefined to equal 'speaking'

AssertionError: expected undefined to equal 'stopped_speaking'


I also tried to use done() insted of the timeout, however the test fails and shows:



ReferenceError: Can't find variable: done


I searched for tutorials, however I can only find simple examples that don't help. How can I write a correct test?


Aucun commentaire:

Enregistrer un commentaire