Boss Machine fails on the test

Boss Machine

Hi,

I finished with the project and at first look everything works fine. I started to test the project and my code fails at several test.
I would like to go step by step.
Is anyone can help me what is incorrect in my code?

First fails:

createMinion
 1) /api/minions routes
       POST /api/minions
         should add a new minion if all supplied information is correct:
     Error: expected 201 "Created", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1572:8)
      at processTicksAndRejections (internal/process/task_queues.js:77:11)
My code:
const createMinion = (req, res, next) => {
    console.log("Being created new minions", req.body);
    const dbResult = db.addToDatabase(modelType, req.body);
    if (dbResult) {
        console.log("dbResult", dbResult);
        res.status(201).json(dbResult);
    } else res.status(404).json({error: "id not found"});
};
// to create a new minion and save it to the database:
minionsRouter.post("/", validateInput, createMinion);

The validateInput method is in a different file:

validateInput.js
const isString = string => Boolean(typeof string === "string" && string.length);

const isInteger = int => Boolean(Number.isInteger(+ int) && int > 0);

const validateInput = (req, res, next) => {
    const dataset = req.body;
    let valid = true;
    for (let key in dataset) {
        // validate strings
        if (  key === "name"
           || key === "title"
           || key === "weaknesses"
           || key === "description") {
            if (!(isString(dataset[key]))) {
                valid = false;
                break;
            }
        }
        
        // validate numbers
        if (   key === "salary"
            || key === "id"
            || key === "minionId"
            || key === "weeklyRevenue"
            || key === "numWeeks"
            || key === "hours") {
            if (!(isInteger(dataset[key]))) {
                valid = false;
                break;
            }
        }
    }
    if (valid) {
        next();
    } else {
        res.status(404).json({error: "invalid data"});
    }
};

const validateId = (req, res, next, id) => {
    id = + id;
    if (Number.isInteger(id) && id > 0) {
        return next();
    }
    res.status(404).json({error: "invalid id"});
};

const validate = {
    validateInput: validateInput,
    validateId: validateId
}

module.exports = validate;

I did not copied the full code because the other parts of code is not relevant. Maybe easier to find the bug… If not, of course I will link the full code.

In the createMinion() function I placed to console.log.
Both returns back the same object (the second one with an id of the new created minions…). I cannot to understand why fails on test?
Maybe my validation is too strict?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.