mardi 1 mars 2016

Mocha testing Data Acess not committing transactions?

I am having a problem with Mocha, I am currently running tests over my application's Data Access modules, and even if they pass the data doesn't seem to be appearing on the database, by the way, the SERIAL id field gets incremented with each test I run.

I tried to test the DAO without mocha, with plain JS code and it works perfectly, now I am wondering if I am the one who's using Mocha the wrong way, probably, or if there's some sort of bug.

this is the code I run with Mocha:

  var chai = require('chai');
  var expect = chai.expect;
  describe('Data Access Users Modules', function() {
  it('should succesfully insert a new user', function() {
    var user = {
      name: 'Testing Mocha',
      email: 'test@mocha.com',
      email_confirmed: 'false'
    }

    users.postUser(user, function(e, r) {
      if (e) done(e);
      if (r) {
        expect(r.rowCount).to.equal(1);
        done();
      };
    });    
  });
});

this is the message I get after execution:

    Data Access Users Modules
this is the postUser function's input data:
{ name: 'Testing Mocha',
  email: 'test@mocha.com',
  email_confirmed: 'false' }
    ✓ should succesfully insert a new user


  1 passing (18ms)

I did a console.log() on the data that's entering the postUser function, that's why you see them.

This, on the other hand, is the code that works and inserts the data in the database:

console.log('TESTING DATA ACCESS');
(function insertUserTest() {
  var chai = require('chai');
  var expect = chai.expect;
  console.log('INSERT')
  var user = {
    name: 'Testing Test',
    email: 'test@test.com',
    email_confirmed: 'false'
  }
  users.postUser(user, function(err, resp) {
    if (err) console.log(err);
    if (resp) {
      expect(resp.rowCount).to.equal(1);
      console.log(resp);
    }
  });
})();

and this is the result I get in the console:

    this is the post user functions data
{ name: 'Testing Test',
  email: 'test@test.com',
  email_confirmed: 'false' }
UPDATE
{ command: 'INSERT',
  rowCount: 1,
  oid: 0,
  rows: [],
  fields: [],
  _parsers: [],
  RowCtor: null,
  rowAsArray: false,
  _getTypeParser: [Function: bound ] }

I would really appreciate your help on this one. Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire