vendredi 24 mars 2017

Testing REST API and rendering with EJS

Consider this scenario:

When you visit my / page the server will respond with a JSON like the following

res.json({
    message: 'Welcome home.'
});

to test this, I create a file home.test.js and test it like so:

const chai = require('chai');
const expect = chai.expect;
const chaiHttp = require('chai-http');
const server = require('../server')

chai.use(chaiHttp);

describe('GET /', () => {
    it('should respond with homepage', (done) => {
        chai
            .request(server)
            .get('/')
            .end((err, res) => {
                expect(res.status).to.equal(200);
                expect(res.body.message).to.equal('Welcome home.');
                done();
            });
    });
});

The simple test passes and everything is going as planned. Here is the problem. Later on, when I want to use ejs to render the view my code is going to change to this:

 res.render('index', { message: 'Welcome home.' });

So now my previous test will not pass as res.body.message will now have the value undefined. How can I actually test this now that the response will be res.render() ?

Aucun commentaire:

Enregistrer un commentaire