0
我们启动了一个节点应用程序,我们将使用Express作为路由器。在我们的控制器之一,我们有这样的定义:如何集成测试节点快递控制器?
var express = require('express');
var router = express.Router();
// api/products
router.get('/', function (req, res) {
res.json({ products: [] });
});
// api/products/:id
router.get('/:id', function (req, res) {
res.json({ id: req.params.id });
});
module.exports = router;
我一直努力遵循有关如何测试这个例子,并想出了:
var hippie = require('hippie');
var server = require('../controllers/products');
describe('Products Controller Tests', function() {
describe('/products endpoint', function() {
it('returns an array of products', function (done) {
hippie(server)
.json()
.get('/products')
.expectStatus(200)
.end(function (err, res, body) {
if (err) throw err;
done();
});
});
});
});
但我总是得到以下错误:
测试名称:产品控制器测试/产品端点返回的产品阵列 测试结果:失败 结果StandardOutput:
不正常1产品控制ler测试/产品端点返回一系列产品 TypeError:无法读取立即在undefined 的属性'apply'。 (C:\阿比\ node_modules \表达\ lib中\路由器\ index.js:618:14)
试验1
通0
失败1
什么是最好的方式单元测试这些控制器?我应该使用不同的库吗?
你想写集成测试或单元测试吗? –
对不起,集成测试 – Josh