Я хочу смоделировать следующий класс, который используется в качестве зависимости для другого:
module.exports = class ResponseHandler {
static response(res, status_, data_, codes_) {
console.log('Im here. Unwillingly');
// doing stuff here...
};
ResponseHandler импортируется ProfileController и используется там:
const response = require('../functions/tools/response.js').response;
module.exports = class ProfileController {
static async activateAccountByVerificationCode(req, res) {
try{
// doing stuff here
return response(res, status.ERROR, null, errorCodes);
}
}
СейчасЯ пишу модульные тесты для ProfileController и там я тестирую, если activAccountByVerificationCode вызывает ответ с заданными аргументами
describe('ProfileController', () => {
let responseStub;
beforeEach(function() {
responseStub = sinon.stub(ResponseHandler, 'response').callsFake(() => null);
});
Но несмотря на то, что ответ является поддельным, ProfileController по-прежнему вызывает реальную реализацию ответа (см. Вывод консоли: «Я здесь. Нежелательно» )
it('should respond accordingly if real verification code does not fit with the one passed by the user', async function () {
// here you can still see that real implementation is still called
// because of console output 'I'm here unwillingly'
await controller.activateAccountByVerificationCode(req, res);
console.log(responseStub.called); // -> false
expect(responseStub.calledWith(res, status.ERROR, null, [codes.INVALID_VERIFICATION_CODE])).to.eql(true); // -> negative
});