2017-02-10 5 views
10

의 기능을 스텁. 그러나 two()을 스터브해야합니다. 유닛 테스트를 수행하면서 two()이 실제 코드에서 실행될 때 발생하는 부작용을 제거해야합니다.내가 단위 테스트에 다음과 같은 단순화 된 모듈을 원하는 proxyquired 객체

it.only('stubbing functions on the "proxyquired" object under test', function(done) { 

    const loggerStub = { 
     create:() => { 
      return { log: (msg) => { console.log('fake logger: ', msg); } }; 
     } 
    }; 

    let tester = proxyquire('../tester', { 'logplease': loggerStub }); 

    let stub2 = sinon.stub(
     tester, 
     'two', 
     () => { 
      console.log('called fake stub of two()'); 
     } 
    ); 

    tester.one(); 

    console.log('call count 2: ', stub2.callCount); 
    done(); 
}); 

출력 내가 얻을 :

fake logger: called real one() 
fake logger: called real two() 
call count 2: 0 

출력 내가 기대 :

fake logger: called real one() 
called fake stub of two() 
call count 2: 1 

왜 스텁 기능이 실행되지 않습니다?

+0

내 답변을 찾았을 수도 있습니다 : http://stackoverflow.com/questions/35111367/test-that-a-function-calls-another-function-in-an-es6-module-with-sinon-js – montrealist

답변

1

짧은 답변 :

const Logger = require('logplease'); 
const logger = Logger.create('utils'); 

const tester = { 

    one:() => { 
     logger.log('called real one()'); 
     tester.two(); 
    }, 
    two:() => { 
     logger.log('called real two()'); 
    }, 
}; 

module.exports = tester; 

설명 : 하나와 두 개의 수출

범위 :이 경우 tester.one에서

module.exports = { 
    one: tester.one, 
    two: tester.two 
}; 

이 기능에 대해 단지 알고 :

two:() => { 
    logger.log('called real two()'); 
} 

스텁에 대해 잘 모른다 . 따라서 두 가지 버전이 있습니다., 안에 tester.two()을 호출하려고합니다.