Preferisco mocha-sinon
sul sinon "semplice" perché si integra perfettamente con Mocha.
Esempio:
var expect = require('chai').expect;
require('mocha-sinon');
// Function to test, can also be in another file and as long as it's
// being called through some public interface it should be testable.
// If it's not in any way exposed/exported, testing will be problematic.
function privateFunction (time) {
if (time < 12) { console.log('Good morning'); }
if (time >= 12 && time <19) { console.log('Good afternoon'); }
else { console.log('Good night!'); }
}
describe('privateFunction()', function() {
beforeEach(function() {
this.sinon.stub(console, 'log');
});
it('should log "Good morning" for hours < 12', function() {
privateFunction(5);
expect(console.log.calledOnce).to.be.true;
expect(console.log.calledWith('Good morning')).to.be.true;
});
it('should log "Good afternoon" for hours >= 12 and < 19', function() {
privateFunction(15);
expect(console.log.calledOnce).to.be.true;
expect(console.log.calledWith('Good afternoon')).to.be.true;
});
it('should log "Good night!" for hours >= 19', function() {
privateFunction(20);
expect(console.log.calledOnce).to.be.true;
expect(console.log.calledWith('Good night!')).to.be.true;
});
});
Un potenziale problema: alcuni reporter moka usano console.log
e, in modo che le prove stub potrebbe non produrre alcun output.
C'è una soluzione alternativa, ma non è l'ideale neanche perché intersputerà l'output Mocha con l'output da privateFunction()
. Se questo non è un problema, sostituire beforeEach()
con questo:
beforeEach(function() {
var log = console.log;
this.sinon.stub(console, 'log', function() {
return log.apply(log, arguments);
});
});
Se si utilizza spia invece di stub, allora la soluzione è non necessario. – TMG
@TMG In realtà sto iniziando a chiedermi se la soluzione alternativa non causerà problemi con le chiamate a 'console.log()' create dai reporter che interferiscono con il conteggio delle chiamate di stub/spia. Probabilmente no, perché penserei che i giornalisti riportino solo dopo che il test è stato eseguito. In ogni caso, usare una spia è davvero una soluzione migliore. – robertklep