2

i am using Express js and Mysql for my application, now i want to start unit testing. I have written this code:

var request = require('supertest');
var server = require('./app');
var chai = require('chai');
var chaiHttp = require('chai-http');

var server = require('./app');

var should = chai.should();
chai.use(chaiHttp);

describe('loading express', function () {

  it('responds to /', function testSlash(done) {
    request(server)
      .get('/')
      .expect(200, done);
  });

  it('404 everything else', function testPath(done) {
    request(server)
      .get('/foo/bar')
      .expect(404, done);
  });

  it('responds to /customers/getCustomerData', function testPath(done) {
    request(server)
      .get('/customers/getCustomerData?id=0987654321')
      .end(function(err, res){
        res.should.have.status(200);
        res.body.should.be.a('object');
        res.body.status.should.equal("success");
        res.body.data.customerId.should.equal("0987654321");

        done();
      });
  });

});

This code is working properly but it makes connection to database and checking all the test cases which i have written, but this is called integration testing i have to do unit testing using mock database. How can i achieve this mocking of database?

5
  • 1
    What do you use to connect to db? Sequelize? Commented Sep 27, 2016 at 11:22
  • No i am using Node ORM2 Commented Sep 27, 2016 at 11:23
  • You have 2 options. One to mock the orm, but better than that use in memory db in tests - sqlite, if your orm supports that. Commented Sep 27, 2016 at 11:27
  • Similar to stackoverflow.com/questions/12526160/… Commented Sep 27, 2016 at 21:27
  • Please provide the code under test. So we can know how to mock your DB. Commented Oct 9, 2020 at 6:56

0

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.