Add Mocha, Chai and Supertest combo for testing

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
This commit is contained in:
Drasko DRASKOVIC 2015-11-01 03:25:29 +01:00
parent 51898276f3
commit 5e31f45b27
4 changed files with 69 additions and 18 deletions

View File

@ -7,7 +7,7 @@ router.route('/')
// get the status (accessed at GET http://localhost:8080/status)
.get(function(req, res) {
var stat = {"status":"OK"};
var stat = {"status":"running"}
res.send(stat);
});

View File

@ -7,7 +7,6 @@
"url": "https://github.com/Mainflux/mainflux"
},
"license": "Apache-2.0",
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
@ -17,8 +16,10 @@
"lodash": "~3.10.1"
},
"devDependencies": {
"chai": "^3.4.0",
"gulp-jshint": "^1.11.2",
"gulp-nodemon": "^2.0.3",
"jshint-stylish": "^2.0.1"
"jshint-stylish": "^2.0.1",
"supertest": "^1.1.0"
}
}

View File

@ -1,4 +1,10 @@
// server.js
/**
* Copyright (c) Mainflux
*
* Mainflux server is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/
/**
* Extrenal configs are kept in the config.js file on the same level
@ -7,31 +13,32 @@ var config = require('./config/config');
console.log(config.message);
// BASE SETUP
// =============================================================================
// call the packages we need
/**
* SETUP
*/
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
// MongoDB
/** MongoDB */
var mongoose = require('mongoose');
// Docker MongoDB url
/** Docker MongoDB url */
var docker_mongo_url = process.env.MAINFLUX_MONGODB_1_PORT_27017_TCP_ADDR
mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name); // connect to our database
/** Connect to DB */
mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name);
// configure app to use bodyParser()
// this will let us get the data from a POST
/** Configure app to use bodyParser() */
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || config.port; // set our port
// ROUTES FOR OUR API
// =============================================================================
/**
* ROUTES
*/
app.use('/status', require('./app/routes/status'));
app.use('/devices', require('./app/routes/devices'));
app.use('/users', require('./app/routes/users'));
@ -39,7 +46,15 @@ app.use('/sessions', require('./app/routes/sessions'));
// START THE SERVER
// =============================================================================
/**
* SERVER START
*/
app.listen(port);
console.log('Magic happens on port ' + port);
/**
* Export app for testing
*/
module.exports = app;

35
test/apiTest.js Normal file
View File

@ -0,0 +1,35 @@
/** Chai stuff */
var should = require('chai').should;
var expect = require('chai').expect;
/** Supertest for API */
var supertest = require('supertest');
var server = require('../server');
var api = supertest(server);
/**
* API test description
*/
describe('loading express', function () {
/**
* /status
*/
it('responds to /status', function testSlash(done) {
api
.get('/status')
.expect(200)
.end(function(err, res){
expect(res.body.status).to.equal("running");
done();
});
});
/**
* /foo/bar
*/
it('404 /foo/bar', function testPath(done) {
api
.get('/foo/bar')
.expect(404, done);
});
});