我是单位测试的新手,所以如果我的问题可能很愚蠢,请原谅我。我使用Mocha和PhantomJS以及Chai作为断言库编写了一个单元测试。我想测试的代码是以下函数:
function speakingNotification(audioStream){
var options = {};
var speechEvents = hark(audioStream, options);
speechEvents.on('speaking', function() {
return 'speaking';
});
speechEvents.on('stopped_speaking', function() {
return 'stopped_speaking';
});
}
如您所见,它将audioStream参数作为输入,然后使用名为hark.js https://github.com/otalk/hark的librabry来检测语音事件。如果用户说话,该函数应该返回。
所以我写了下面的单元测试:
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');
});
});
});
测试失败并显示:
AssertionError: expected undefined to equal 'speaking'
AssertionError: expected undefined to equal 'stopped_speaking'
我还尝试使用done(),但是测试失败并显示:
ReferenceError: Can't find variable: done
我搜索了教程,但是我只能找到不起作用的简单示例。 我怎样才能写出正确的测试?