Updated controller logic and reworked unit testing
This commit is contained in:
+36
-33
@@ -1,9 +1,17 @@
|
||||
require("../mongoConfig")
|
||||
|
||||
const routes = require('../routes/msg')
|
||||
|
||||
const request = require('supertest');
|
||||
const server = require('../server');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
app.use(express.urlencoded({ extended: false }))
|
||||
app.use("/", routes)
|
||||
|
||||
describe('GET /msg', function () {
|
||||
it('respond with json containing a list of all users', function (done) {
|
||||
request(server)
|
||||
request(app)
|
||||
.get('/msg')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('Content-Type', /json/)
|
||||
@@ -13,24 +21,19 @@ describe('GET /msg', function () {
|
||||
|
||||
describe('GET /msg/:id', function () {
|
||||
it('respond with json containing a single message', function (done) {
|
||||
request(server)
|
||||
request(app)
|
||||
.get('/msg/1')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Testing get a message endpoint by giving a non-existing message
|
||||
*/
|
||||
describe('GET /msg/:id', function () {
|
||||
it('respond with json message not found', function (done) {
|
||||
request(server)
|
||||
.get('/users/idisnonexisting')
|
||||
//.set('Accept', 'application/json')
|
||||
.expect(404) //expecting HTTP status code
|
||||
//.expect('"Message not found"') // expecting content value
|
||||
request(app)
|
||||
.get('/msg/idisnonexisting')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(400) //expecting HTTP status code
|
||||
.expect('Content-Type', /json/) // expecting content value
|
||||
.end((err) => {
|
||||
if (err) return done(err);
|
||||
done();
|
||||
@@ -42,30 +45,30 @@ describe('GET /msg/:id', function () {
|
||||
* Testing post message endpoint
|
||||
*/
|
||||
describe('POST /msg', function () {
|
||||
const data = {
|
||||
Message: "test",
|
||||
}
|
||||
|
||||
it('respond with 201 created', function (done) {
|
||||
request(server)
|
||||
.post('/msg/1/test')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end((err) => {
|
||||
if (err) return done(err);
|
||||
done();
|
||||
});
|
||||
request(app)
|
||||
.post('/msg/1')
|
||||
.type('form')
|
||||
.send(data)
|
||||
.then(() => {
|
||||
request(app)
|
||||
.get('/msg/1')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200, done)
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Testing post message endpoint
|
||||
*/
|
||||
describe('POST /msg', function () {
|
||||
it('respond with 400 not created', function (done) {
|
||||
request(server)
|
||||
.post('/msg/test')
|
||||
it('respond with 400 not updated', function (done) {
|
||||
request(app)
|
||||
.post('/msg/1')
|
||||
.set('Accept', 'application/json')
|
||||
//.expect('Content-Type', /json/)
|
||||
.expect(404)
|
||||
//.expect('"Message not created"')
|
||||
.expect(400)
|
||||
.expect('Content-Type', /json/)
|
||||
.end((err) => {
|
||||
if (err) return done(err);
|
||||
done();
|
||||
|
||||
Reference in New Issue
Block a user