vendredi 1 mai 2020

Testing router+res.render functions with Chai

My main.js looks like this:

'use strict';
const express = require( 'express' );
const app = express();
const homeController = require( './controllers/homeController' );
const errorController = require( './controllers/errorController')

console.log("+get homeController");
app.get( '/', homeController );

console.log('+get  homeController.showUsers');
app.get( '/users', homeController.showUsers );

In homeController.js there is this router.get method I want to test with Chai:

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', {
    title: 'Internship Approval System'
  });
});

My router.test.js file looks like this:

var sinon = require('sinon');
var chai = require('chai');
var expect = chai.expect;

var router = require('./controllers/homeController');

describe('home route', function() {
  it('should return rendered response', function() {
    var req = {};
    var res = {
      render: sinon.spy()
    };

    // Also return the promise here, and add an assertion to the chain.
    return router.get(req, res).then(function() {
      expect(res.render.calledOnce).to.be.true;
    });
  });
});

I'm getting an error when trying to see if it's called once:

  1) home route
       should return a rendered response:
     Error: Route.get() requires a callback function but got a [object Object]
      at Route.<computed> [as get] (node_modules\express\lib\router\route.js:202:15)

      at Function.proto.<computed> [as get] (node_modules\express\lib\router\index.js:510:19)
      at Context.<anonymous> (router.test.js:15:19)
      at processImmediate (internal/timers.js:439:21)

Aucun commentaire:

Enregistrer un commentaire