简体   繁体   中英

Testing Promises with multiple thens using testdoublejs

I am using testdouble for stubbing calls within my node.js project. This particular function is wrapping a promise and has multiple then calls within the function itself.

function getUser (rethink, username) {
  return new Promise((resolve, reject) => {
  let r = database.connect();
  r.then(conn => database.table(tablename).filter({username}))
   .then(data => resolve(data))
   .error(err => reject(err));
 });
}

So I am wanting to determine if the resolve and reject are handled correctly based on error conditions. Assume there is some custom logic in there that I need to validate.

For my test

import getUser from './user';
import td from 'testdouble';
test(t => {
  const db = td.object();
  const connect = td.function();
  td.when(connect('options')).thenResolve();
  const result = getUser(db, 'testuser');
  t.verify(result);
}

The issue is that the result of connect needs to be a promise, so I use then resolve with a value which needs to be another promise that resolves or rejects.

The line it is relating to is the result of database.connect() is not a promise.

TypeError: Cannot read property 'then' of undefined

Anyone have success with stubbing this type of call with Test Double?

So figured out the resolution. There are a few things to note in the solution and that we encountered. In short the resolution ended up being this...

td.when(database.connect()).thenResolve({then: (resolve) => resolve('ok')});

This resolves a thenable that is returned when test double sees database connect. Then subsequent calls can also be added.

There is also a part to note if you send in an object to database.connect() you have to be aware that it is doing === equality checking and you will need to have a reference to that object for it to correctly use td.when .

Test double provides stubs for unit testing. And in your case 'db' is the object we need to mock. Creating the mocking db through

td.object(Database) // Database is the class or constructor of your db

will be the right choice, but to simply mock those methods you need in this case, I wouldn't pick that way.

Here's the tested module, 'some.js':

function getUser (database, username) {
  return new Promise((resolve, reject) => {
    let r = database.connect();
    r.then(conn => database.table('table').filter({username:username}))
      .then(data => resolve(data))
      .catch(err => reject(err));
  });
}

module.exports = getUser;

And the test file, using mocha and chai.expect, which is could also be any other unit test module here:

let td = require('testdouble');
let expect = require('chai').expect;

const getUser = require('./some');

describe('some.js',()=>{
  it('getUser',()=>{
    const db = {};
    const name = 'name';
    db.connect = td.function();
    db.table = td.function('table');
    db.filter = td.function('filter');
    td.when(db.connect()).thenResolve(db);
    td.when(db.table('table')).thenReturn(db);
    td.when(db.filter({username: name})).thenResolve('some user data');
    return getUser(db, name)
      .then(user=>{
        expect(user).to.equal('some user data')
      })
      .catch(e=>assert(e))
  })
})

So please let me know if any of these confuse you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM