Nodejs Mocha:无法通过ID测试POST和GET

时间:2019-03-29 03:42:25

标签: javascript node.js mocha supertest

尝试测试POST请求和ID GET。对于POST错误,它指出:“预期200,得到400”。然后,对于3个GET by ID,前两个是“错误:超时超过2000毫秒......”,然后在几分钟后给了我两个ID。然后是第三个:“预期404,得到400”。

试图查看文档以获取期望,超级测试和摩卡,但找不到解决方案。那三是我用于这次测试的

这是POST测试

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));
            });
    });
});

然后按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();
            });
    });
});

这里是我的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);
  });
}

这里是我按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);
  });
};

预期会通过,但就像我说的那样,结果是: 1)POST:“错误:预期为200,得到400” 2)通过ID进行的前两个GET:“错误:超过了2000ms的超时时间。 ....' 3)按ID的最后一次获取:“预期404,得到400”

1 个答案:

答案 0 :(得分:0)

400错误请求错误是HTTP状态代码,表示您发送到服务器的请求在某种程度上不正确或已损坏,服务器无法理解。 尝试检查您的架构,如果错过了抛出400的东西,则应该发布所有必填项。