我正在创建此测试文件
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('../index');
chai.use(chaiHttp);
describe('Users', () => {
describe('/GET users', () => {
it('it should GET all the users', (done) => {
chai.request(server).get('/user/get-all-users').end((err, res) => {
res.should.have.status(200)
done()
});
});
});
});
这条路线运行得很好,但在测试运行时我总是得到测试失败404
我正在使用restify来运行我的服务器,
global.server = restify.createServer({
url : config.base_url,
name : config.name,
version : config.version,
log : bunyanWinston.createAdapter(log),
})
答案 0 :(得分:2)
我想通了
这是我的服务器:
'use strict'
/**
* Module Dependencies
*/
const config = require('./config'),
restify = require('restify'),
bunyan = require('bunyan'),
winston = require('winston'),
bunyanWinston = require('bunyan-winston-adapter'),
mongoose = require('mongoose')
/**
* Logging
*/
global.log = new winston.Logger({
transports: [
new winston.transports.Console({
level: 'info',
timestamp: () => {
return new Date().toString()
},
json: true
}),
]
})
/**
* Initialize Server
*/
global.server = restify.createServer({
url : config.base_url,
name : config.name,
version : config.version,
log : bunyanWinston.createAdapter(log),
})
/**
* Middleware
*/
server.use(restify.jsonBodyParser({ mapParams: true }))
server.use(restify.acceptParser(server.acceptable))
server.use(restify.queryParser({ mapParams: true }))
server.use(restify.fullResponse())
/**
* Error Handling
*/
server.on('uncaughtException', (req, res, route, err) => {
log.error(err.stack)
res.send(err)
});
/**
* Lift Server, Connect to DB & Bind Routes
*/
server.listen(config.port, 'localhost', function() {
mongoose.connection.on('error', function(err) {
log.error('Mongoose default connection error: ' + err)
process.exit(1)
})
mongoose.connection.on('open', function(err) {
if (err) {
log.error('Mongoose default connection error: ' + err)
process.exit(1)
}
log.info(
'%s v%s ready to accept connections on port %s in %s environment. %s',
server.name,
config.version,
config.port,
config.env,
server.url
)
require('./api/routes/')
})
global.db = mongoose.connect(config.db.uri)
})
module.exports = server
我要求回调listen函数中的路由文件。这种方式的路线有效,但柴无法找到路线。只需从回调中删除路由并将其放在我的index.js上,它就可以正常工作。