With sinon, you can put a mock or stub around an entire module. For example, suppose the mysql
module has a function query
:
var mock;
mock = sinon.mock(require('mysql'))
mock.expects('query').with(queryString, queryParams).yields(null, rows);
queryString
, queryParams
are the input you expect. rows
is the output you expect.
When your class under test now require mysql and calls the query
method, it will be intercepted and verified by sinon.
In your test expectation section you should have:
mock.verify()
and in your teardown you should restore mysql back to normal functionality:
mock.restore()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…