Sails js ::testing传递给视图的variables
调节器
module.exports = { index: function (req, res) { res.view({message: 'hello'}); } }
如何testingvariablesmessage
是否设置正确?
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
返回{}
您正在testing响应主体是否具有message
属性 ,但这不是视图本地人如何工作的方式:它们作为视图引擎可以replace的variables传递给视图。 如果variables不存在于您的视图中,replace将不会发生。 而且,在supertest中使用get
方法默认不会返回一个res.body
; 你需要检查res.text
。 所以:
- 确保您的
home/index.ejs
视图实际上包含<%= message %>
某处。 - 把你的testing
assert(res.text.match(/hello/))
来检查是否存在应该在渲染视图中replace<%= message %>
的“hello”string。
传递给你的视图函数的参数看起来不正确。 它看起来像你试图返回一个JSON对象。 尝试使用json函数:
res.json({message: 'hello'});
如果你想将这个对象传递给应用程序中的视图文件,你需要在函数的第一个参数中指定视图文件的path:
res.view("viewfile", {message: 'hello});