How to unit test a function which calls another that returns a promise?

jbernal

I have a node.js app using express 4 and this is my controller:

var service = require('./category.service');

module.exports = {
  findAll: (request, response) => {
    service.findAll().then((categories) => {
      response.status(200).send(categories);
    }, (error) => {
      response.status(error.statusCode || 500).json(error);
    });
  }
};

It calls my service which returns a promise. Everything works but I am having trouble when trying to unit test it.

Basically, I would like to make sure that based on what my service returns, I flush the response with the right status code and body.

So with mocha and sinon it looks something like:

it('Should call service to find all the categories', (done) => {
    // Arrange
    var expectedCategories = ['foo', 'bar'];

    var findAllStub = sandbox.stub(service, 'findAll');
    findAllStub.resolves(expectedCategories);

    var response = {
       status: () => { return response; },
       send: () => {}
    };
    sandbox.spy(response, 'status');
    sandbox.spy(response, 'send');

    // Act
    controller.findAll({}, response);

    // Assert
    expect(findAllStub.called).to.be.ok;
    expect(findAllStub.callCount).to.equal(1);
    expect(response.status).to.be.calledWith(200); // not working
    expect(response.send).to.be.called; // not working
    done();
});

I have tested my similar scenarios when the function I am testing returns itself a promise since I can hook my assertions in the then.

I also have tried to wrap controller.findAll with a Promise and resolve it from the response.send but it didn't work neither.

Johannes Merz

You should move your assert section into the res.send method to make sure all async tasks are done before the assertions:

var response = {
   status: () => { return response; },
   send: () => {
     try {
       // Assert
       expect(findAllStub.called).to.be.ok;
       expect(findAllStub.callCount).to.equal(1);
       expect(response.status).to.be.calledWith(200); // not working
       // expect(response.send).to.be.called; // not needed anymore
       done();
     } catch (err) {
       done(err);
     }
   },
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

How to test a function which returns a promise $q?

How to test a function which calls another function when doing unit tests?

Python Unit test how to test a method which calls another method

How to use another promise in a function which returns a new promise?

How to write Unit test for a method which returns anonymous function

Unit Test function that returns Mongoose promise

Jest unit testing a function that calls a function that returns a promise

Sinon unit testing. How to unit test a function that will return a promise that will call another function with callback?

How to unit test javascript function that calls getJSON

How to unit test a function returning a Promise with then block

How to write test for a function which calls another function using jest and enzyme?

Jest how to test the line which calls a function?

How to unit test a function which as viewchild

How to test function which returns function with parameters?

Jasmine, AngularJS: Unit test a function which returns a value after timeout

How to unit test android app which requires a web service calls

How to unit test a method which calls a void method?

How to write a unit test for a method which calls other services

How to test changes made by onClick event that calls a setState function, which is passed from another component and changes UI?

How do I Unit Test an Async method that returns a Promise

How to add unit test for a function that calls a function received in props

How to handle Action which returns Accumultor[ByteString,Result] in unit test

How to create a unit test for an IActionResult controller action which returns decimal?

How to Unit Test a GlassController Action which Returns a View Taking a Model

Mapping a promise which returns foo, to another promise which returns bar?

How to unit test a function in a component that calls method to parent component

How to unit test express middleware function that calls res.cookie

How efficiently unit test a function with multiple input calls?

TOP Ranking

HotTag

Archive