multiple request for each object in an array
I'm trying to test an api. Here's how it works.
It will create 20 documents in the database (for loop). Like this one.
{
"_id": {
"$oid": "5bfa89a329359318e15a4acc"
},
"text": "praiseworthy",
"bumped_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"reported": false,
"delete_password": "123",
"created_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"replies": ,
"__v": 0
}
Then it will get the first 10 documents through the
it('GET')test where it will be save inbodyvariable as array.
For each object in that array it has a
repliesproperty the same to the one above. Then for each object it should call/post a reply. Wherein it will add document/s (an object) inside thereplies. The number of objects inside thereplieswill depend the result of thelet randomNumberOfReplies = Math.floor(Math.random() * 5) + 1. So, it should like this:
replies: [{
"_id": {
"$oid": "5bfa8d79f17ea01e886af976"
},
"text": "priceless",
"delete_password": "222",
"reported": false,
"created_on": {
"$date": "2018-11-25T11:54:33.235Z"
}
}]
Here is the actual code for test:
const chaiHttp = require('chai-http');
const chai = require('chai');
const superb = require('superb');
const mongoose = require('mongoose');
const request = require('request');
const assert = chai.assert;
const server = require('../server');
chai.use(chaiHttp);
describe('API ROUTE FOR /api/threads/:board', function() {
for (let i = 0; i < 20; i++) {
it('POST', (done) => {
chai.request(server)
.post('/api/threads/211')
.send({ text: superb.random(), delete_password: '123' })
.end((err, res) => {
if (err) done(err);
assert.equal(res.status, 200);
// done();
})
});
}
it('GET', function(done) {
chai.request(server)
.get('/api/threads/xx')
.end(function(err, result){
let body = result.body; // returns an array of objects
body.forEach((elem) => {
let randomNumberOfReplies = Math.floor(Math.random() * 5) + 1;
for (let i = 0; i < randomNumberOfReplies; i++ ){
chai.request(server)
.post('/api/replies/new')
.type('form')
.send({ text: superb.random(), delete_password: '222', thread_id: elem._id })
.end((err, res) => {
if (err) console.log(err);
// console.log(res);
// done();
})
}
})
console.log('done');
done();
})
});
})
api testing mocha chai chai-http
add a comment |
I'm trying to test an api. Here's how it works.
It will create 20 documents in the database (for loop). Like this one.
{
"_id": {
"$oid": "5bfa89a329359318e15a4acc"
},
"text": "praiseworthy",
"bumped_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"reported": false,
"delete_password": "123",
"created_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"replies": ,
"__v": 0
}
Then it will get the first 10 documents through the
it('GET')test where it will be save inbodyvariable as array.
For each object in that array it has a
repliesproperty the same to the one above. Then for each object it should call/post a reply. Wherein it will add document/s (an object) inside thereplies. The number of objects inside thereplieswill depend the result of thelet randomNumberOfReplies = Math.floor(Math.random() * 5) + 1. So, it should like this:
replies: [{
"_id": {
"$oid": "5bfa8d79f17ea01e886af976"
},
"text": "priceless",
"delete_password": "222",
"reported": false,
"created_on": {
"$date": "2018-11-25T11:54:33.235Z"
}
}]
Here is the actual code for test:
const chaiHttp = require('chai-http');
const chai = require('chai');
const superb = require('superb');
const mongoose = require('mongoose');
const request = require('request');
const assert = chai.assert;
const server = require('../server');
chai.use(chaiHttp);
describe('API ROUTE FOR /api/threads/:board', function() {
for (let i = 0; i < 20; i++) {
it('POST', (done) => {
chai.request(server)
.post('/api/threads/211')
.send({ text: superb.random(), delete_password: '123' })
.end((err, res) => {
if (err) done(err);
assert.equal(res.status, 200);
// done();
})
});
}
it('GET', function(done) {
chai.request(server)
.get('/api/threads/xx')
.end(function(err, result){
let body = result.body; // returns an array of objects
body.forEach((elem) => {
let randomNumberOfReplies = Math.floor(Math.random() * 5) + 1;
for (let i = 0; i < randomNumberOfReplies; i++ ){
chai.request(server)
.post('/api/replies/new')
.type('form')
.send({ text: superb.random(), delete_password: '222', thread_id: elem._id })
.end((err, res) => {
if (err) console.log(err);
// console.log(res);
// done();
})
}
})
console.log('done');
done();
})
});
})
api testing mocha chai chai-http
add a comment |
I'm trying to test an api. Here's how it works.
It will create 20 documents in the database (for loop). Like this one.
{
"_id": {
"$oid": "5bfa89a329359318e15a4acc"
},
"text": "praiseworthy",
"bumped_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"reported": false,
"delete_password": "123",
"created_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"replies": ,
"__v": 0
}
Then it will get the first 10 documents through the
it('GET')test where it will be save inbodyvariable as array.
For each object in that array it has a
repliesproperty the same to the one above. Then for each object it should call/post a reply. Wherein it will add document/s (an object) inside thereplies. The number of objects inside thereplieswill depend the result of thelet randomNumberOfReplies = Math.floor(Math.random() * 5) + 1. So, it should like this:
replies: [{
"_id": {
"$oid": "5bfa8d79f17ea01e886af976"
},
"text": "priceless",
"delete_password": "222",
"reported": false,
"created_on": {
"$date": "2018-11-25T11:54:33.235Z"
}
}]
Here is the actual code for test:
const chaiHttp = require('chai-http');
const chai = require('chai');
const superb = require('superb');
const mongoose = require('mongoose');
const request = require('request');
const assert = chai.assert;
const server = require('../server');
chai.use(chaiHttp);
describe('API ROUTE FOR /api/threads/:board', function() {
for (let i = 0; i < 20; i++) {
it('POST', (done) => {
chai.request(server)
.post('/api/threads/211')
.send({ text: superb.random(), delete_password: '123' })
.end((err, res) => {
if (err) done(err);
assert.equal(res.status, 200);
// done();
})
});
}
it('GET', function(done) {
chai.request(server)
.get('/api/threads/xx')
.end(function(err, result){
let body = result.body; // returns an array of objects
body.forEach((elem) => {
let randomNumberOfReplies = Math.floor(Math.random() * 5) + 1;
for (let i = 0; i < randomNumberOfReplies; i++ ){
chai.request(server)
.post('/api/replies/new')
.type('form')
.send({ text: superb.random(), delete_password: '222', thread_id: elem._id })
.end((err, res) => {
if (err) console.log(err);
// console.log(res);
// done();
})
}
})
console.log('done');
done();
})
});
})
api testing mocha chai chai-http
I'm trying to test an api. Here's how it works.
It will create 20 documents in the database (for loop). Like this one.
{
"_id": {
"$oid": "5bfa89a329359318e15a4acc"
},
"text": "praiseworthy",
"bumped_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"reported": false,
"delete_password": "123",
"created_on": {
"$date": "2018-11-25T11:38:11.121Z"
},
"replies": ,
"__v": 0
}
Then it will get the first 10 documents through the
it('GET')test where it will be save inbodyvariable as array.
For each object in that array it has a
repliesproperty the same to the one above. Then for each object it should call/post a reply. Wherein it will add document/s (an object) inside thereplies. The number of objects inside thereplieswill depend the result of thelet randomNumberOfReplies = Math.floor(Math.random() * 5) + 1. So, it should like this:
replies: [{
"_id": {
"$oid": "5bfa8d79f17ea01e886af976"
},
"text": "priceless",
"delete_password": "222",
"reported": false,
"created_on": {
"$date": "2018-11-25T11:54:33.235Z"
}
}]
Here is the actual code for test:
const chaiHttp = require('chai-http');
const chai = require('chai');
const superb = require('superb');
const mongoose = require('mongoose');
const request = require('request');
const assert = chai.assert;
const server = require('../server');
chai.use(chaiHttp);
describe('API ROUTE FOR /api/threads/:board', function() {
for (let i = 0; i < 20; i++) {
it('POST', (done) => {
chai.request(server)
.post('/api/threads/211')
.send({ text: superb.random(), delete_password: '123' })
.end((err, res) => {
if (err) done(err);
assert.equal(res.status, 200);
// done();
})
});
}
it('GET', function(done) {
chai.request(server)
.get('/api/threads/xx')
.end(function(err, result){
let body = result.body; // returns an array of objects
body.forEach((elem) => {
let randomNumberOfReplies = Math.floor(Math.random() * 5) + 1;
for (let i = 0; i < randomNumberOfReplies; i++ ){
chai.request(server)
.post('/api/replies/new')
.type('form')
.send({ text: superb.random(), delete_password: '222', thread_id: elem._id })
.end((err, res) => {
if (err) console.log(err);
// console.log(res);
// done();
})
}
})
console.log('done');
done();
})
});
})
api testing mocha chai chai-http
api testing mocha chai chai-http
edited Nov 25 '18 at 12:08
isemaj
asked Nov 25 '18 at 6:30
isemajisemaj
156
156
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465210%2fmultiple-request-for-each-object-in-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465210%2fmultiple-request-for-each-object-in-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown