0

I have this module and the corresponding test with proxyquire.

// sqlQuery.js
const sql = require('mssql');
module.exports = function sqlFn() {
  const request = new sql.Request();
}

// sqlQueryTest.js
const proxyquire = require('proxyquire');
const sql = require('mssql');
let sqlStub = sinon.stub(sql);

describe('Sql wrapper', function() {

  let sqlQuery = proxyquire('../public/sqlQuery', {
    'sql': sqlStub
  })
  sqlQuery()

  it('Should call sql once', function() {
    expect(sqlStub.Request.callCount).to.deep.equal(1) // passes
  })

})

If in the sqlQuery I want to use the request object's functions, the test won't run:

TypeError: request.query is not a function

I tried rewire as well to set require to a stub:

let stub1 = sinon.stub()
let sqlQuery = rewire('../public/sqlQuery')
sqlQuery.__set__('request', {
  input: stub1
})

How can I test if request is called with given methods in the code?

jota
  • 3
  • 2
  • What is sql and how does it generate request? Is it mssql? That means you probably have to mock the sql.createRequest (or whatever method creates the request object) and return a mock of the request. OO and mocking a big pain but if it's mssql you are using it seems someone did [most of the work for you](https://stackoverflow.com/a/38646049/1641941). – HMR Feb 06 '18 at 16:21
  • Yes, it's mssql. I was hoping there is a better way of doing this, but the answer that @HMR linked looks like what I need. – jota Feb 07 '18 at 11:18

0 Answers0