meeran jasir
meeran jasir

Reputation: 89

How to write post request to node js server using Mocha and what are the js Needed for that

I am new to Mocha and Chai testing Framework ,i am referring the tutorial here i Understand it and its very Good for beginners but What this tutorial is it get the requests via url and in my condition i want to post and pick those data in my node server i cannot found anything unitl now so help me what are the needs and what are files to install in npm. And please send me the useful Links for Beginners Tutorials. And if may a sample application with node and mocha post requests..

Upvotes: 1

Views: 5887

Answers (2)

meeran jasir
meeran jasir

Reputation: 89

We can also use the chai-http for post requests in Mocha

below is My Solution

var chai = require('chai'), chaiHttp = require('chai-http');
chai.use(chaiHttp);
var app = 'localhost:3000';

describe("Sample Unit Testing", function() {
    describe("Get User Data", function() {
        it("get The User Employee ID", function(done) {
            // Send some Form Data
             chai.request(app)
            .post('/getUserData')
            .send({
            password: '3333', 
            empId: '1111'
            })
            .end(function (err, res) {
                expect(res.EmpId).to.equal("1111");               
                done();
            });
        });

    });
});

Upvotes: 3

meeran jasir
meeran jasir

Reputation: 89

This is the Code I am Looking For and I found out myself

//you must install these two in your node js using npm
var expect  = require("chai").expect;
var request = require('superagent');

describe("Name For The Test Suite", function() {
    it("Testing Post Request using Mocha", function(done) {
        request.post('localhost:8081/yourRequestCatcherName')
        .set('Content-Type', 'application/json')
        .send('{"fieldName":"data"}')
        .end(function(err,res){
            //your code to Test
            done();
        })
    });        
});

it Works Perfectly in the way What I want.

Upvotes: 1

Related Questions