zoukankan      html  css  js  c++  java
  • [Unit testing] Simplify Assertions on Error Messages with toMatchInlineSnapshot

    Let’s add a test for an edge case that responds with an error message. In this lesson we’ll talk about the value of using the toMatchInlineSnapshot assertion for error messages.

    Code:

    async function createListItem(req, res) {
      const {
        user: {id: ownerId},
      } = req
      const {bookId} = req.body
      if (!bookId) {
        res.status(400).json({message: `nwo bookId provided`})
        return
      }
      const [existingListItem] = await listItemsDB.query({ownerId, bookId})
      if (existingListItem) {
        res.status(400).json({
          message: `User ${ownerId} already has a list item for the book with the ID ${bookId}`,
        })
        return
      }
    
      const listItem = await listItemsDB.create({ownerId, bookId})
      res.json({listItem: await expandBookData(listItem)})
    }

    Test:

    test('createListItem return a 400 error with no bookId', async () => {
      const req = buildReq()
      const res = buildRes()
    
      await listItemsController.createListItem(req, res)
    
      expect(res.status).toHaveBeenCalledWith(400)
      expect(res.status).toHaveBeenCalledTimes(1)
      //expect(res.json).toHaveBeenCalledWith({message: `No bookId provided`})
      expect(res.json.mock.calls[0]).toMatchInlineSnapshot(`
        Array [
          Object {
            "message": "no bookId provided",
          },
        ]
      `)
      expect(res.json).toHaveBeenCalledTimes(1)
    })
  • 相关阅读:
    FPGA 在线调试方法概述
    Quartus II& Nios II 出错解决办法
    Verilog HDL--VGA显示
    常用电平转换方案
    Verilog HDL SPI通信——读
    Verilog HDL SPI通信——写
    Verilog HDL串口发送程序
    verilog HDL 串口接受程序
    关于AFNetworking中header的bug问题
    iOS项目的本地化处理(多国语言)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/15147805.html
Copyright © 2011-2022 走看看