Hi everyone,
I am almost finished with the Issue Tracker project in the IS/QA section. I am finishing the last functional test for deleting an issue when given a valid _id. For the query, I could pass a string with the _id but once the test runs and deletes the item from the database, it will fail the test on any subsequent runs. My idea is to (possibly with middleware?) get an array of all the issues in the database and use the _id from the first item in the returned array as the _id to delete in the test; therefore, if there is always at least one item in the database, then all of my tests will pass. The code I currently have for my test is
test('Valid _id', function(done) {
chai.request(server)
.delete('/api/issues/test')
.query({_id: /* id string */ })
.end((err,res)=>{
assert.equal(res.status, 200);
assert.equal(res.body.success, 'deleted ' + /*id string*/);
done();
})
});
Is there a way for me to query the database for an array of all the issues and get the _id of the first returned item before I run the DELETE request?
Edit:
So here is what I ended up doing to get it to work.
First, I actually send some data to my POST endpoint to create a document specifically for this test
test('Valid _id', function(done) {
chai.request(server)
.post('/api/issues/test')
.send({
issue_title: 'Delete Test',
issue_text: 'testing delete with valid _id',
created_by: 'test suite'
})
Then, I save the _id from the response JSON into a variable to send to my DELETE endpoint:
.end(function(err,res){
var _idToDelete = res.body._id;
chai.request(server)
.delete('/api/issues/test')
.send({_id: _idToDelete})
.end(function(err,res){
assert.equal(res.status, 200);
assert.equal(res.body.success, 'deleted ' + _idToDelete);
console.log(res.body);
done();
})
})