首页 > 其他分享 >[Unit testing Express] Test Express route

[Unit testing Express] Test Express route

时间:2022-08-22 19:23:48浏览次数:69  
标签:route Express item mockController path Test router method

server.js:

import itemRouter from './resources/item/item.router'
export const app = express()
app.use('/api/item', itemRouter)

item.router.js

import { Router } from 'express'
const router = Router()

const mockController = (req, res) => {
  res.json({ message: 'ok' })
}
// /api/item
router
  .route('/')
  .get(mockController)
  .post(mockController)

// /api/item/:id
router
  .route('/:id')
  .get(mockController)
  .put(mockController)
  .delete(mockController)

export default router

 

test:

import router from '../item.router'

describe('item router', () => {
  test('has crud routes', () => {
    const routes = [
      { path: '/', method: 'get' },
      { path: '/:id', method: 'get' },
      { path: '/:id', method: 'delete' },
      { path: '/:id', method: 'put' },
      { path: '/', method: 'post' }
    ]

    routes.forEach(route => {
      const match = router.stack.find(
        s => s.route.path === route.path && s.route.methods[route.method]
      )
      expect(match).toBeTruthy()
    })
  })
})

 

标签:route,Express,item,mockController,path,Test,router,method
From: https://www.cnblogs.com/Answer1215/p/16613950.html

相关文章

  • AtCoder Beginner Contest 265赛后总结
    生日打了场AtcoderBeginner还可以吧……做出了前四道题,第五、六题是dp方程没想出来QwQA-Apple水题+1,感谢atcoder把坑都亮出来QwQ……分两种情况讨论:三个一卖的比(一个......
  • testng基础知识:注解的执行顺序
    testng基础知识:注解的执行顺序1.单类,无继承父子关系code:1publicclassbasicTest{2@BeforeSuite(alwaysRun=true)3publicvoidbeforeSuite_b......
  • 【pytest】内置fixture之request
    前言:request是pytest的内置fixture,"为请求对象提供对请求测试上下文的访问权,并且在fixture被间接参数化的情况下具有可选的“param”属性。"1、request.param前......
  • AtCoder Beginner Contest 264(D-E)
    D-"redocta".swap(i,i+1)题意:给一个字符串,每次交换相邻两个字符,问最少多少次变成"atcoder"题解:从左到右依次模拟#include<bits/stdc++.h>usingnamespacestd;......
  • unittest框架与生成报告
    一、unittest与HTMLtestrunner生成带日志的测试报告下载项目https://github.com/huilansame/htmltestRunner_PY3#gthub项目地址项目解析说明importunittestfrom......
  • [题解] Atcoder Regular Contest ARC 146 A B C D 题解
    点我看题A-ThreeCards先把所有数按位数从多到少排序,答案的位数一定等于位数最多的三个数的位数之和\(tot\)。对于每个i,把有i位的数排序,并记录每个i的排序结果。最后......
  • 新建Springboot项目默认test包下的测试类报错缺少org.junit.jupiter.api
    参考CSDN博客链接:https://blog.csdn.net/weixin_39764056/article/details/104402593本人使用IDEA新建了一个Springboot项目,然后导入了spring-boot-start-test依赖,该项目......
  • SAS | TTEST
    利用汇总统计量进行成组t检验建立数据集datadt1;inputtype$gain@@;datalines;a45a62a96a128a120a99a28a50a109a115a......
  • 【笔记】greatest/least函数&Round函数
    刷了一下力扣,发现有很多的函数是自己不清楚的,用了这些函数是比较容易得出结果的,不用自己费心去实现一些奇怪的东西1.最大最小值链接:https://leetcode.cn/problems/number......
  • Pytest框架 — 14、Pytest的标记(五)(控制测试用例执行顺序)
    目录1、前言2、使用3、标记最先执行和最后执行1、前言在执行自动化测试时,我们通常都希望能够控制执行测试用例的顺序。在unittest框架中默认按照ACSII码的顺序加载测试......