Насмешка с помощью node.js намного проще, чем Java, благодаря гибкости javascript.
Вот полный пример насмешки над классом со следующим классом:
// lib/accountManager.js
class AccountManager {
create (name) {
this._privateCreate(name);
}
update () {
console.log('update')
}
delete () {
console.log('delete')
}
_privateCreate() {
console.log('_privateCreate')
}
}
module.exports = AccountManager
Вы можете издеватьсяэто выглядит так:
// test/accountManager.test.js
const
sinon = require('sinon'),
should = require('should')
AccountManager = require('../lib/accountManager');
require('should-sinon'); // Required to use sinon helpers with should
describe('AccountManager', () => {
let
accountManager,
accountManagerMock;
beforeEach(() => {
accountManagerMock = {
_privateCreate: sinon.stub() // Mock only desired methods
};
accountManager = Object.assign(new AccountManager(), accountManagerMock);
});
describe('#create', () => {
it('call _privateCreate method with good arguments', () => {
accountManager.create('aschen');
should(accountManagerMock._privateCreate).be.calledOnce();
should(accountManagerMock._privateCreate).be.calledWith('aschen');
})
});
});
Здесь вы можете найти больше примеров насмешливых классов и зависимостей: https://github.com/Aschen/workshop-tdd/blob/master/step2/test/file.test.js