我想今天开始测试快速路线,但我可以弄清楚如何测试渲染玉视图。
这是我的代码:
路线:
router.get('/', function(req: any, res: any) {
res.render('index', { title: 'Express' });
});
测试:
describe('GET / ', () => {
it('renders index', (done) => {
request(router)
.get('/')
.render('index', { title: 'Express' })
.expect(200, done);
});
});
当然.render
会导致错误。我该如何测试渲染?
答案 0 :(得分:0)
您可能需要在测试中配置渲染引擎。检查回复body
是否有Error: No default engine was specified and no extension was provided.
。
我能够使用:
// Setup Fake rendering
beforeEach(() => {
app.set('views', '/path/to/your/views');
app.set('view engine', 'ext');
app.engine('ext', (path, options, callback) => {
const details = Object.assign({ path, }, options);
callback(null, JSON.stringify(details));
});
}
it('your awesome test', async () => {
const res = await agent.get('/route').type('text/html');
// This will have your response as json
expect(res.text).to.be.defined;
});
答案 1 :(得分:0)
您可以改用chai
const chai = require('chai');
const chaiHttp = require('chai-http');
const expect = chai.expect;
chai.use(chaiHttp);
describe('Route Index', () => {
it('should render the index view with title', (done) => {
chai.request(app)
.get('/')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res).to.have.header('content-type', 'text/html; charset=utf-8');
expect(res.text).to.contain('Express');
done();
});
});
});