单元测试快速路由回调 - 模拟 res 对象
Unit testing express route callback - mock res object
我正在努力了解如何对呈现 html 文件的简单快速获取请求进行单元测试。
在我的设置中,当我收到有关未设置视图引擎的错误时,当我 运行 我的代码时。
我想做的只是简单地检查 'res.render' 函数是否已被调用并模拟它,因此它实际上不会执行它应该执行的操作。
route.js
module.exports = function(router) {
router.get('/test', function(req, res, next) {
console.log("Hi");
res.render('test/test', { current: "current"});
});
};
test.js
const request = require('supertest');
const sinon = require('sinon');
const express = require('express');
const app = express();
const routes = require('../../routes/route')(app);
describe('My routes', function () {
before(function() {
return this.spy = sinon.spy(app, 'render');
});
after(function() {
return this.spy.restore();
});
it('should render to /test', function(done) {
var res = { render: sinon.spy() };
request(app)
.get('/test')
.expect(200, done)
});
});
它在 res.render('test/test') 行失败,我实际上不希望执行此行我想以某种方式模拟它?
我想我遗漏了一些明显的东西!
谢谢大家
乔伊
您收到此错误的原因应该很清楚,因为您在尝试 运行 render()
方法时没有在您的 Express 应用程序上设置 view engine
。
来自关于模板引擎的 Express 文档(显然将以下内容替换为您在应用程序中使用的引擎):
app.set('views', './views') // specify the views directory
app.set('view engine', 'ntl') // register the template engine
至于模拟渲染方法,您可能想看看 https://github.com/danawoodman/sinon-express-mock 在 Express 中模拟您的请求和响应对象。即使您不想使用那个包,您也可以查看源代码以了解他们是如何做的,以及这是否符合您想要完成的目标。
问题是你创建了一个全新的 Express 应用程序,而你应该 "requiring" 一个在你的真实代码中其他地方使用的现有应用程序。您的路线存在于真正的快递应用程序中,因此获得它们的唯一方法是测试真正的快递应用程序。在您当前的设置中,即使您通过设置所有需要的属性来解决错误,您仍然需要复制您在真正的快递应用程序中放置的所有内容。
从 test.js
中删除
const express = require('express');
const app = express();
上面的代码正在创建一个空应用程序,这不是您要测试的内容。
替换为
const app = require('../../../app').app //customize for your file system
上面的代码是一个示例,但想法是您的 const app
被测需要指向您实际应用程序中的 express 实例。因此,如果您的主应用程序文件是 server.js 并且它比您的测试文件高 3 个级别,那么在您的路由测试中您需要使用
const app = require('../../../server.js).app
假设在内部 server.js 你调用了你的 express 实例应用程序。
我正在努力了解如何对呈现 html 文件的简单快速获取请求进行单元测试。
在我的设置中,当我收到有关未设置视图引擎的错误时,当我 运行 我的代码时。
我想做的只是简单地检查 'res.render' 函数是否已被调用并模拟它,因此它实际上不会执行它应该执行的操作。
route.js
module.exports = function(router) {
router.get('/test', function(req, res, next) {
console.log("Hi");
res.render('test/test', { current: "current"});
});
};
test.js
const request = require('supertest');
const sinon = require('sinon');
const express = require('express');
const app = express();
const routes = require('../../routes/route')(app);
describe('My routes', function () {
before(function() {
return this.spy = sinon.spy(app, 'render');
});
after(function() {
return this.spy.restore();
});
it('should render to /test', function(done) {
var res = { render: sinon.spy() };
request(app)
.get('/test')
.expect(200, done)
});
});
它在 res.render('test/test') 行失败,我实际上不希望执行此行我想以某种方式模拟它?
我想我遗漏了一些明显的东西!
谢谢大家 乔伊
您收到此错误的原因应该很清楚,因为您在尝试 运行 render()
方法时没有在您的 Express 应用程序上设置 view engine
。
来自关于模板引擎的 Express 文档(显然将以下内容替换为您在应用程序中使用的引擎):
app.set('views', './views') // specify the views directory
app.set('view engine', 'ntl') // register the template engine
至于模拟渲染方法,您可能想看看 https://github.com/danawoodman/sinon-express-mock 在 Express 中模拟您的请求和响应对象。即使您不想使用那个包,您也可以查看源代码以了解他们是如何做的,以及这是否符合您想要完成的目标。
问题是你创建了一个全新的 Express 应用程序,而你应该 "requiring" 一个在你的真实代码中其他地方使用的现有应用程序。您的路线存在于真正的快递应用程序中,因此获得它们的唯一方法是测试真正的快递应用程序。在您当前的设置中,即使您通过设置所有需要的属性来解决错误,您仍然需要复制您在真正的快递应用程序中放置的所有内容。
从 test.js
中删除const express = require('express');
const app = express();
上面的代码正在创建一个空应用程序,这不是您要测试的内容。
替换为
const app = require('../../../app').app //customize for your file system
上面的代码是一个示例,但想法是您的 const app
被测需要指向您实际应用程序中的 express 实例。因此,如果您的主应用程序文件是 server.js 并且它比您的测试文件高 3 个级别,那么在您的路由测试中您需要使用
const app = require('../../../server.js).app
假设在内部 server.js 你调用了你的 express 实例应用程序。