0

In My NodeJS Application, Connecting to Sql-DB using tedious, I want to unit test the microservices related to DB. During the unit test I don't want connect real database. But I am getting the error

Error: Unable to connect database: Error:'.connect cannot be called on a connection in SentLogin7WithStandardLogin state

Can you recommend any suggestions?

1 Answer 1

0

I recommend using sinon to mock/stub database connection. It works if you use ORM library or native databas library. I usually use stub method like this:

const sinon = require(‘sinon’);
const chai = require(‘chai’);

it(‘sentence for expected test’, () => {
   let stubDbConnection = sinon.stub(dbLib, ‘connectionMethod’);
   testedFunction();

   chai.expect(stubDbConnection.calledOnce).to.be.true;
});

Hope it helps you. Regards

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.