簡體   English   中英

Javascript rest api 測試

[英]Javascript rest api tests

我在這里有一個小學校項目,顯然在節點 js 中實現了一些簡單的 http 動詞。 問題是我定義了一些測試(所有測試都與最后一個 POST 方法相關),並且在五個測試中,只有兩個通過並且知道為什么。 誰能告訴我應該如何修改該帖子才能滿足測試? 第一個代碼是app js文件,第二個是服務器端。

const express = require('express');
    const bodyParser = require('body-parser');
    const Sequelize = require('sequelize');
    const { STRING, INTEGER } = require('sequelize');

    const sequelize = new Sequelize({
      dialect: 'sqlite',
      storate: 'homework.db'
    });

    const Student = sequelize.define('student', {
      name: Sequelize.STRING,
      address: Sequelize.STRING,
      age: Sequelize.INTEGER
    }, {
      timestamps: false
    });

    const app = express();
    app.use(bodyParser.json());

    app.get('/create', async (req, res) => {
      try {
        await sequelize.sync({ force: true });
        for (let i = 0; i < 10; i++) {
          const student = new Student({
            name: 'name ' + i,
            address: 'some address on ' + i + 'th street',
            age: 30 + i
          });
          await student.save();
        }
        res.status(201).json({ message: 'created' });
      } catch (err) {
        console.warn(err.stack)
        res.status(500).json({ message: 'server error' });
      }
    });

    app.get('/students', async (req, res) => {
      try {
        const students = await Student.findAll();
        res.status(200).json(students);
      } catch (err) {
        console.warn(err.stack);
        res.status(500).json({ message: 'server error' });
      }
    });

    app.post('/students', async (req, res) => {
      try {
        //TODO
        if (req.body !== null) {
          const student = await Student.create(req.body);
          if (!(typeof(student.name) === STRING && typeof(student.address) === STRING && typeof(student.age) === INTEGER)) {
            if (student.age > 0) {
              return res.status(201).json({ message: "created" });
            } else {
              res.status(400).json({ message: "age should be a positive number" });
            }
          } else {
            res.status(400).json({ message: "malformed request" });
          }
        } else {
          res.status(400).json({ message: "body is missing" });
        }
      } catch (err) {
        console.warn(err.stack);
        res.status(500).json({ message: 'server error' });
      }
    });

    module.exports = app;

測試如下:

  • 如果請求正文未發送,服務器應以狀態碼 400 和 {"message":"body is missing} 響應 -> 失敗

  • 如果請求正文存在但未遵循指定格式,服務器應以狀態碼 400 和 {"message":"malformed request"} -> failed 響應

  • 年齡不應該是負數 -> 通過

  • 可以添加一個學生 -> 通過

  • 學生名單有效 -> 失敗

const app = require('./app')

app.listen(8080, () => {
  console.log('Server started on port 8080...')
})

首先,不要使用bodyParser.json() 它已被棄用。 請改用express.json

到目前為止我所看到的是您濫用typeof運算符,它不返回參數的實際類型的實例,只是一個描述類型的字符串,例如( typeof "hello world"返回"string" )所以typeof任何數字永遠不會返回Number類型,只是"number"字符串,因此typeof student.age === INTEGER )將始終為假。

所以修復你至少可以通過前 3 個失敗的測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM