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:
- make sure
home/index.ejsview contains<%= message %>somewhere. - change test
assert(res.text.match(/hello/))check presence of "hello" string should replace<%= message %>in rendered view.
Comments
Post a Comment