0

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 https://github.com/otalk/hark 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?

ltedone
  • 639
  • 1
  • 6
  • 12
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Louis Feb 15 '15 at 12:25
  • The problem in what you are showing us here is with how you coded the code in your first code snippet. If you read the answers to the other question, you'll learn what you did wrong. – Louis Feb 15 '15 at 12:27

0 Answers0