如何使用 Mocha 向节点 js 服务器写入 post 请求以及需要什么 js
How to write post request to node js server using Mocha and what are the js Needed for that
我是 Mocha 和 Chai 测试框架的新手,我指的是教程 here 我理解它并且它对初学者非常有用但是这个教程是什么它通过 url 和在我的情况下,我想 post 并在我的节点服务器中选择这些数据我现在找不到任何单元所以帮助我需要什么以及在 npm 中安装什么文件。请将有用的初学者教程链接发给我。并且如果可以使用节点和 mocha post 请求的示例应用程序..
这是我正在寻找的代码,我自己找到了
//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();
})
});
});
它以我想要的方式完美运行。
我们还可以在 Mocha
中对 post 请求使用 chai-http
下面是我的解决方案
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();
});
});
});
});
我是 Mocha 和 Chai 测试框架的新手,我指的是教程 here 我理解它并且它对初学者非常有用但是这个教程是什么它通过 url 和在我的情况下,我想 post 并在我的节点服务器中选择这些数据我现在找不到任何单元所以帮助我需要什么以及在 npm 中安装什么文件。请将有用的初学者教程链接发给我。并且如果可以使用节点和 mocha post 请求的示例应用程序..
这是我正在寻找的代码,我自己找到了
//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();
})
});
});
它以我想要的方式完美运行。
我们还可以在 Mocha
中对 post 请求使用 chai-http下面是我的解决方案
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();
});
});
});
});