简体   繁体   中英

Unit Testing w/ Sinon: How do I test a variable inside of a Callback?

I a doing unit test with Sinon to at least try to get something to pass and am out of luck. I wasn't able to find anything online.

I have 2 problems:

1) I am trying to get a simple response, but it fails saying spy is false . When I log it, it says it's true .

2) I want to test the template in the callback function is equal to 'New Title'. I am unable to figure how to pass req, res, and next into router.get

router.js

const express = require('express'), router = express.Router();

router.get('/', (req, res, next) => {
var template = {
  title: req.title,
  item: req.item,
  description: 'N/A'
};
res.render('index', template);

module.exports = router;
});

router.spec.js

const router = require('./router');
var res = {};
var req = {
  title: 'New Title',
  item: 'Blank Item'
};
var template = {
  title: req.title,
  item: req.item,
  description: 'N/A'
};


it('1) should call the response', ()=>{
 var spy = sinon.spy(route, 'get');
 route.get('/', ()=>{});
 console.log(spy.calledOnce); // equals true
 assert(spy.calledOnce).toBeTruthy();
 spy.restore();
});

it('2) should return a status 200', ()=>{
 var stub = sinon.stub().yield([req, res, next]);
 route.get('/', stub); // main problem
 assert.equal(200, res.statusCode);
 stub.restore();
});

I want to access the route handler so I can do further testing. Where and how can I implement route handling in this situation?

Express app for routing is usually best to test with integration testing not unit testing that you attempted to do. Supertest can be used as the library.

Another improvement that we can do is to separate the logic to generate template into another file. And for this service, we can make a unit test on it. Use mocha or jest can be the option.

Example:

const request = require('supertest');
const express = require('express');
const service = require('./service'); // our new service file
const app = express();

app.get('/', function(req, res) {
  const template = service.getTemplate(req);
  res.render('index', template);
});

// integration test, ideally must be separated into a test file e.g. app-integration-test.js

request(app)
  .get('/')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  .end(function(err, res) {
    if (err) throw err;
  });


// service.js

function getTemplate(req, res) {
  return {
    title: req.title,
    item: req.item,
    description: 'N/A'
  };
}

// service-unit-test.js

describe('service test', function() {
  it('returns template correctly', function() {
    const req = {
      title: 'someone',
      item: 'my item',
    }
    const template = service.getTemplate(req);

    expect(template).to.eql({
      title: 'someone',
      item: 'my item',
      description: 'N/A'
    })
  })
})

Hope it helps!

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