In recent months, I have been working with JavaScript and used SinonJS to drown out some kinds of behavior. I managed to get it working, I skipped a lot of methods, and everything works fine.
But I still have some questions about how Sinon works under the table. I think I'm telling Sinon, but this question can be applied to all other libraries designed for bullying / stub / spy.
The language I have been working on the most in recent years has been Java. In Java, I used Mockito to bully / stub dependencies and inject dependencies. I used to import a class, annotate the field with @Mockand pass this layout as a parameter to the tested class. It's easy for me to understand what I'm doing: mock the class and skip the layout as a parameter.
When I first started working with SinonJS, I saw something like this:
moduleUnderTest.spec.js
const request = require('request')
describe('Some tests', () => {
let requestStub
beforeEach(() => {
requestStub = sinon.stub(request, 'get')
})
afterEach(() => {
request.get.restore()
})
it('A test case', (done) => {
const err = undefined
const res = { statusCode: 200 }
const body = undefined
requestStub
.withArgs("some_url")
.yields(err, res, body)
const moduleUnderTest = moduleUnderTest.someFunction()
// some assertions
})
})
moduleUnderTest.js
const request = require('request')
request
.get("some_url", requestParams, onResponse)
And it works. When we run the tests, requestinside the implementation it moduleUnderTest.jscalls an enveloped version of the module request.
My question is: why does this work?
, request. Sinon ( mock/stub/spy) , ( )? Sinon request ( ) , require('request'), ?
stub.js Sinon repo, JavaScript,
, .:)