我嘗試使用Mocha,Chai和Supertest編寫單元測試,但不幸的是,現在不成功。
我旁邊路線:NodeJs:接下來不是功能
var express = require('express');
var GoogleUrl = require('google-url');
var _ = require('lodash');
var token = require('../middlewares/token');
var Url = require('../models/url');
var config = require('../config');
var router = express();
router.post('/urls/create', token.required, createShortUrl);
module.exports = router;
function createShortUrl(req, res) {
_.trim(req.body.list_tags);
var tags = _.split(req.body.list_tags, ',');
tags.splice(tags.length - 1, 1);
var date = returnDate();
var time = returnTime();
var googleUrl = new GoogleUrl({
'key': config.get('google_key')
});
googleUrl.shorten(req.body.full_url, function (err, shortUrl) {
if (err) {
res.status(500).json(err);
}
var url = new Url({
'author': req.payload.username,
'description': req.body.description,
'full_url': req.body.full_url,
'short_url': shortUrl,
'list_tags': tags,
'date': date,
'time': time
});
url.save(function (err) {
if (err) {
res.status(500).json(err);
} else {
res.status(200).json('Shortener url is created!');
}
});
});
}
function returnDate() {
var d = new Date();
var yyyy = d.getFullYear();
var mm = d.getMonth() < 9 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
var dd = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
return dd + '-' + mm + '-' + yyyy;
}
function returnTime() {
var d = new Date();
var hh = d.getHours() < 10 ? '0' + d.getHours() : d.getHours();
var min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
var ss = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
return hh + ':' + min + ':' + ss;
}
我節省使用貓鼬在MongoDB中的數據。 對於路線我寫的單元測試:
var app = require('../server');
var chai = require('chai');
var request = require('supertest');
var jwt = require('express-jwt');
var config = require('../config');
var expect = chai.expect;
describe('Urls Tests', function() {
var url = {
author : 'Alexey',
description : 'grrggr',
full_url : 'https://github.com',
short_url : 'short_url_by_google',
date : '30-06-2017',
time : '18:21:27',
count_click : 0,
list_tags : [
'Sport',
'Football'
]
};
var token = {
required: jwt({
secret: config.get('jwtSecret'),
userProperty: 'payload',
getToken: 'token',
})
};
describe('## Create url ', function() {
it('should create a url', token.required, function(done) {
request(app)
.post('/urls/create')
.send(url)
.end(function(err, res) {
expect(res.statusCode).to.equal(200);
expect(res.body.author).to.equal('Alexey');
url = res.body;
done();
});
});
});
});
當我運行單元測試我得到一個錯誤:
TypeError: next is not a function
at Context.middleware (node_modules\express-jwt\lib\index.js:76:16)
我不明白爲什麼我得到它。請幫幫我 ! 我認爲我創造了很少的錯誤,因爲可以找到。
修訂
middlewares/token.js
:
var jwt = require('express-jwt');
var config = require('../config');
function getTokenFromHeader(req){
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Token') {
return req.headers.authorization.split(' ')[1];
}
return null;
}
var token = {
required: jwt({
secret: config.get('jwtSecret'),
userProperty: 'payload',
getToken: getTokenFromHeader
})
};
module.exports = token;
我發佈了我的'middleware/token'文件。 –