Boss Machine POST Testing Error

Heya, I’m going through the Boss Machine project (link) and everything is going well, save for a single error that doesn’t seem right to me. During the tests i receive the following:

POST /api/minions
{ name: 'Test', title: '', salary: 0, weaknesses: '' }
      1) should add a new minion if all supplied information is correct
 

1) /api/minions routes
       POST /api/minions
         should add a new minion if all supplied information is correct:

      AssertionError: expected { Object (name, title, ...) } to deeply equal {}
      + expected - actual

      -{
      -  "id": [undefined]
      -  "name": "Test"
      -  "salary": 0
      -  "title": ""
      -  "weaknesses": ""
      -}
      +{}

      at D:\WebProjects\project-4-boss-machine-start\test\test.js:194:46
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

I tried POSTing that exact object, which worked in the app, but i received the same error from the test. I also tried adding logic to prevent adding and object like this, thinking the test was worded incorrectly, and got the error i expected, but still failed the test. Has anyone else run into this problem? I wonder if my best bet is fiddling with the test itself. Here’s the code to my minion.js file:

const express = require('express');
const minionsRouter = express.Router();

const {
    getAllFromDatabase,
    getFromDatabaseById,
    addToDatabase,
    updateInstanceInDatabase,
    deleteFromDatabasebyId,
} = require('./db');

minionsRouter.param('minionId', (req, res, next, id) => {
  const minionId = id;
  const allMinions = getAllFromDatabase('minions');
  if(isNaN(minionId) || minionId > allMinions.length) {
    res.sendStatus(404).send();
    return;
  }
  next();
});

minionsRouter.get('/', (req, res) => {
  const allMinions = getAllFromDatabase('minions');
  res.send(allMinions);
});

minionsRouter.post('/', (req, res) => {
  console.log(req.body);
  const minionBody = req.body;
  addToDatabase('minions', minionBody);
  res.sendStatus(201).send();  
});


minionsRouter.get('/:minionId', (req, res) => {
  const minionId = req.params.minionId;
  const minion = getFromDatabaseById('minions', minionId);
  res.send(minion);
});

Thanks in advance.

Looks like you’re really close.

You need to respond to the POST with the newly created record that addToDatabase() created for you. That helper function returns what you need.

1 Like

Oh, wow, I totally overlooked the fact that my .send() was empty. I got locked into res.status(404).send() mode. Thanks for the second eyes.