我是测试的新手,我可以毫无问题地用邮递员达到终点,但是mocha似乎无法识别依赖于文件系统的变量,它说未定义callBackPath和appPath
app.use((req, res, next) => {
let version = req.url.match(/\/api\/(v[0-9]+).*/) || [];
const {
readdirSync
} = require('fs');
const dirPath = path.join(__dirname, './api');
const getDirectories = srcPath => fs.readdirSync(srcPath).filter(file => fs.statSync(path.join(srcPath, file)).isDirectory());
const lastDir = getDirectories(dirPath)[getDirectories(dirPath).length - 1];
version = version[1] || '';
if (version != '') {
const appPath = path.join(__dirname, `./api/${version}/index.js`);
const callBackPath = path.join(__dirname, `./api/${lastDir}/index.js`);
console.log(callBackPath);
if (!fs.existsSync(appPath)) {
return res.status(404).send({
message: 'It\'s not us, sorry we can\'t find this end point'
});
}
require(appPath)(app);
} else {
require(callBackPath)(app);
}
next();
});
app.listen(config.port);
console.log(`Server started on port ${
config.port
}`);
module.exports = app;
摩卡代码
chai.use(chaiHttp);
chai.should();
describe('user', ()=>{
describe('POST /', ()=> {
it("should create new user", done=>{
chai.request(app)
.post('/register')
.end((err, res) => {
res.should.have.status(200);
res.should.be.a('object');
done();
});
});
});
});