node.js - Sails js :: Testing variables passed to views -


controller

module.exports = {    index: function (req, res) {     res.view({message: 'hello'});   } } 

how test if variable message been set correctly?

var request = require('supertest');  describe('homecontroller', function() {   describe('index', function() {     it('should return success', function (done) {       request(sails.hooks.http.app)         .get('/')         .expect(200).end(function (err, res) {           if (err) throw err;           res.body.should.have.property('message');           done();         });     });   }); }); 

res.body returns {}

you testing response body has property of message, that's not how view locals work: passed view variables view engine can replace. if variable isn't present in view, replacement won't happen. furthermore, using get method in supertest won't return res.body default; you'll need check res.text instead. so:

  1. make sure home/index.ejs view contains <%= message %> somewhere.
  2. change test assert(res.text.match(/hello/)) check presence of "hello" string should replace <%= message %> in rendered view.

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -