Nodejs Mocha: Unable to test a POST and GET by ID

问题: Trying to test a POST request and GET by ID. For the POST error it states: "expected 200, got 400". Then for the 3 GET by IDs, the first two is "Error: Timeout of 2000ms ex...

问题:

Trying to test a POST request and GET by ID. For the POST error it states: "expected 200, got 400". Then for the 3 GET by IDs, the first two is "Error: Timeout of 2000ms exceeded......", then gives me the two IDs a few minutes later. Then the third: "Expected 404, got 400".

Tried looking at docs for expect, supertest and mocha and couldnt find a solution. Those 3 is what i use for this testing

Here is the POST test

describe('POST /drinks', () => {
    it('should create a new drink', (done) => {
        let type = 'coffee';
        let name = 'testName';
        let image = 'testImage';

        request(app)
            .post('/drinks')
            .send({
                type,
                name,
                image
            }).expect(200).expect((res) => {
                expect(res.body.type, res.body.name, res.body.image).toBe(text);
            }).expect((res) => {
                expect(res.body.rating).toBe(number);
            }).end((err, res) => {
                if (err) {
                    return done(err);
                }

                Coffee.find({
                    type
                }).then((feedData) => {
                    expect(feedData.length).toBe(1);
                    expect(feedData[0].type).toBe(text);
                    done();
                }).catch(e => done(e));
            });
    });
});

Then heres the GET by ID:

describe('GET /drinks/:id', () => {
    it('should return individual drink document', (done) => {
        request(app)
            .get(`/drinks/${feedData[0]._id.toHexString()}`)
            .expect(200)
            .expect(res => {
                expect(res.body.drink.text).toBe(feedData[0].text);
            })
            .end((err, res) => {
                if (err) return done(err);
                done();
            });
    });

    it('should return 404 if drink is not found', (done) => {
        let hexId = new ObjectID().toHexString();

        request(app)
            .get(`/drinks/${hexId}`)
            .expect(404)
            .end((err, res) => {
                if (err) return done(err);
                done();
            });
    });

    it('should return 404 for non-object ids', (done) => {
        request(app)
            .get('/drinks/123abc')
            .expect(404)
            .end((err, res) => {
                if (err) return done(err);
                done();
            });
    });
});

Heres my route for POST:

// POST a drink
exports.postDrinks = (req, res) => {
  let type = req.body.type;
  if (!type) {
    res.status(400).send('Request parameters missing');
  }
  let newDrink;

  // Default Drink Fields
  let defaultFields = {
    type,
    name: req.body.name,
    tastingNotes: req.body.tastingNotes,
    comments: req.body.comments,
    image: req.body.image,
    rating: req.body.rating
  }

  // Determine which type and store it as that type
  switch (type) {
    case 'beer':
      newDrink = new Beer({
        ...defaultFields,
        style: req.body.style,
        source: req.body.source,
      });
      break;
    case 'coffee':
      newDrink = new Coffee({
        ...defaultFields,
        beanType: req.body.beanType,
        brewTime: req.body.brewTime,
        strength: req.body.strength
      });
      break;
    case 'liquor':
      newDrink = new Liquor({
        ...defaultFields,
        typOfLiquor: req.body.typOfLiquor
      });
      break;
    case 'tea':
      newDrink = new Tea({
        ...defaultFields,
        leafType: req.body.leafType,
        steepTime: req.body.steepTime,
      });
      break;
    default:
      console.log('Please select an apprioriate drink');
      break;
  }

  // Saves POST and sends it back as well. If not, then error
  newDrink.save().then((drink) => {
    res.send(drink);
  }, (e) => {
    res.status(400).send(e);
  });
}

Heres my route for GET by ID:

/ GET by ID
exports.getIndividualDrink = (req, res) => {
  let id = req.params.id;

  // Show everything but id and v
  Drink.findById(id).select('-_id -__v').then((drink) => {

    // Check if theres that drink and ID is valid
    if (!drink && !ObjectID.isValid(id)) {
      return res.status(401).send();
    }

    // If there is, then send it back
    res.send({
      drink
    });
  }, (e) => {
    res.status(400).send(e);
  });
};

Expected should be passing, but like i said the results are: 1) POST: 'Error: expected 200, got 400' 2) First two GET by ID: 'Error: Timeout of 2000ms exceeded. ....' 3) Last GET by ID: 'Expected 404, got 400'


回答1:

The 400 Bad Request error is an HTTP status code that means that the request you sent to the server, was somehow incorrect or corrupted and the server couldn't understand it. Try to check your schema, you should post all required item if you miss something 400 is thrown.

  • 发表于 2019-03-30 06:44
  • 阅读 ( 190 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除