我正在使用Mocha,Chai和Chai HTTP在JavaScript CRUD应用程序上运行测试。该应用程序使用Sequelize。所有测试的功能都在浏览器中正确GET和POST。但是,我的所有测试都失败了,状态为500.当我尝试获取未包含在我的应用程序中的路由时,我获得了404状态,因此我非常确定服务器已正确连接到测试。有什么想法吗?
这是测试套件:
//test suite for user controller
process.env.NODE_ENV = 'test';
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../server');
const should = chai.should();
const models = require('../models');
const bcrypt = require('bcryptjs');
chai.use(chaiHttp);
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};
let currentDate = new Date();
describe('Users', function() {
beforeEach('clear and add', function(done) {
let newUser = {
email: 'susan@example.com',
password: 'spaz5713'
}
models.User.sync({ force: true })
.then(function() {
return models.User
.create(newUser)
.then(function(data) {
console.log(data);
});
});
let newSession = {
sid: 'WDfAv8WICmE_gyrEjckhG_a9ijWNekZD',
data: '"cookie":{"originalMaxAge":259200000,"expires":"' + currentDate.addDays(3) + '","httpOnly":true,"path":"/"},"email":"susan@example.com","password":"' + bcrypt.hashSync("spaz5713", bcrypt.genSaltSync(10)) + '"'
}
models.ConnectSession.sync({force: true})
.then(function() {
return models.ConnectSession
.create(newSession)
.then(function(data) {
console.log(data);
done();
});
});
});
it('should render the landing page', function(done) {
chai.request(server)
.get('/')
.end(function(err, res) {
console.log(res);
res.should.have.status(200);
res.should.be.html;
done();
});
});
it('should get signup page /user/signup GET', function(done) {
chai.request(server)
.get('/user/signup')
.end(function(err, res) {
console.log(res);
res.should.have.status(200);
done();
});
});
it('should create new user /user/signup POST', function(done) {
let newUser = {
'email': 'alice@example.com',
'password': 'spaz5713'
}
chai.request(server)
.post('/user/signup')
.send(newUser)
.end(function(err, res) {
console.log(res);
res.should.have.status(200);
done();
});
});
//etc
});
这是我的服务器:
// ++++++ dev tools ++++++
const util = require('util');
// ++++++ General Express config ++++++
const express = require('express'),
bodyParser = require('body-parser'),
logger = require('morgan'),
hbs = require('express-handlebars'),
cookieParser = require('cookie-parser'),
app = express(),
routes = require('./routes/index');
app.use(logger("dev"));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static("./public"));
//++++++ Handlebars config ++++++
app.engine('hbs', hbs({
extname: 'hbs',
defaultLayout: 'main',
layoutsDir: __dirname + '/views/layouts/',
partialsDir: __dirname + '/views/partials/'
}));
app.set('view engine', 'handlebars');
//++++++ express sessions ++++++
const session = require('express-session'),
database = require('./models').sequelize,
SequelStore = require('connect-sequelize')(session),
secret = require('./config/secret'),
modelName = 'ConnectSession';
app.use(session({
secret: secret,
store: new SequelStore(database, {}, modelName),
resave: true,
saveUninitialized: true
}));
//++++++ Routes ++++++
app.use(routes);
// ++++++ SERVER LISTEN ++++++
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, function () {
const host = server.address().address;
const port = server.address().port;
console.log('express server listening to your mom at http://' + host + ':' + port);
});
module.exports = server;