Tag: 超级

在Mocha + Supertest的404testing中出现“Body Parse”错误

我正在尝试编写testing以确保我的Express API正确地为各种情况返回正确的HTTP状态代码。 我在testing中使用Mocha和Supertest来请求API。 现在我得到了非常意想不到的结果,下面会详细介绍。 使用:Express,body-parser,Sequelize,Mocha,Supertest GET / users /:id models.User.find(req.params.id).complete(function(err, foundUser) { if (err) { logger.error(err, 'Error'); return err; } console.log('user: ' + foundUser); if (foundUser != null) { res.json({ user: foundUser.getJsonRepresentation() }); } else { res.status(404); res.json({ error: 'Not found' }); } }); testing这种方法 it('responds with the right user', function(done){ request(app) .get(apiPath + '/users/' […]

在Express中testing中间件时提供会话

我尝试使用supertest和nock在我的Express应用程序中testing中间件function,但遇到了问题,我设置的路由由中间件的前面部分检查,以确保传入的req对象上存在会话属性。 我需要在运行testing之前以某种方式模拟会话对象,但不确定如何处理这个问题。 首先让我分享一些代码: router.js app.use('/api', helpers.hasAccessToken, require('./routes.js')); routes.js router.route('/*') .get(routesController.fetch) helpers.js module.exports.hasAccessToken = function(req, res, next) { if(req.session.accessToken){ next(); } else { res.status(401).send('LOGIN_SESSION_ENDED'); } }; routesController.js module.exports.fetch = function(req, res) { var options = helpers.buildAPIRequestOptions(req); request(options, function(err, response, body){ res.status(response.statusCode).send(body); }); }; routesController.spec.js var app = require('./index.js'), request = require('supertest')(app), expect = require('chai').expect, nock = […]

如何使用ES6 super在子构造函数中调用父方法?

情况: 我使用其他属性(timestamp,id)来扩展Node.js(v.8.4.0)Error对象,然后扩展这个对象以获得更精细的error handling。 class MyError extends Error { constructor (msg) { super(msg); this.id = uuid(); this.timestamp = Date.now(); // I reckon this can be replaced by this.init(this) ? this.name = this.constructor.name; Error.captureStackTrace && Error.captureStackTrace(this, this.constructor); } init (self) { self.name = self.constructor.name; Error.captureStackTrace && Error.captureStackTrace(self, self.constructor); } } 我希望能够在子错误中不重复Error.captureStackTrace和this.name调用。 所以我创build了一个我在孩子中使用的init函数: class GranularError extends MyError { […]

Supertest不返回结果,节点实例不退出

我只是在学习testing,我已经在这里几个小时没有成功。 每次我运行Mocha ,我的testing显示在terminal,并没有返回结果,如下所示: Express test app started on port: 3001 addition 之后它保持空白。 而且,节点进程在testing之后不会closures,当它们堆积起来时,会使我的计算机崩溃。 这是我的样本testing: var supertest = require('supertest'); var should = require('should'); process.env.NODE_ENV = 'test'; var app = require('../server'); describe('addition', function() { //… previous test it('should return 2 given the url /add/1/1', function(done) { request(app) .get('/add/1/1') .expect(200) .end(function(err, res) { should.not.exist(err); parseFloat(res.text).should.equal(2); done(); }); }); […]

为什么地址在我的应用程序中未定义?

我有一个简单的快速应用程序: var express = require('express'); var path = require('path'); var app = express(); exports.app = app; var index = require('./routes/index'); app.use(express.static(path.join(__dirname,'client/dist/'))); app.get('/', index.get); function start(){ var port = process.env.PORT || 8080; app.listen(port, function(){ console.log('app is running on port: ' + port); }); }; exports.start = start; 而一个整合testing: var request = require('supertest'); var app = require('../app'); […]