如何对调用 res.cookie 的 Express 中间件函数进行单元测试

Der*_*nel 5 unit-testing mocha.js express sinon chai

我正在尝试使用 mocha、chai 和 sinon 对我自己的 Express 中间件功能进行单元测试。

基本上,该函数检查token上的参数req.query,如果存在,则添加一个值为 的 cookie token

function tokenRedirect(req, res, next) {
    const qs = require('querystring');

    if (req.query && req.query.token) {
        res.cookie('token', req.query.token);
    } else {
        next();
        return;
    }

    res.redirect('/');
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以做这样的事情:

res = { cookie: sinon.spy() };
Run Code Online (Sandbox Code Playgroud)

进而

assert(res.cookie.calledWith('token', expectedToken));
Run Code Online (Sandbox Code Playgroud)

但我想做的是检查是否req.cookies确实拥有我期望的 cookie,如下所示:

assert.equal(req.cookies.token, expectedToken);
Run Code Online (Sandbox Code Playgroud)

问题是resreq、 以及它们的所有属性都只是我为测试创建的模拟,因此并不是res.cookie真正向对象添加 cookie 的实际 Express 函数req

如何将真实的 Expressres对象传递给我的函数?有可能吗?

我希望我足够清楚,单元测试对我来说是一个新概念。

Fra*_*teo 4

使用来自 Express 的实际对象req和对象会更容易受到集成测试的影响(如果我错了,请纠正我)。res单元测试仅关注您的代码,所有其他外部依赖项都应该被模拟,如果我在评论中错了,请再次纠正我。

您需要部分模拟/实现 Express在此处使用的逻辑,而不是模拟reswith 。所以类似(使用 Jest):sion

const tokenRedirect = require('../location/to/middleware/tokenRedirect')

describe('tokenRedirect', () => {
  /**
   * Mocked Express Request object.
   */
  let req

  /**
   * Mocked Express Response object.
   */
  let res

  /**
   * Mocked Express Next function.
   */
  let next = jest.fn()

  beforeEach(() => {
    req = {
      body: {}
    }

    res = {
      query: {},
      headers: {},
      data: null,
      json(payload) {
        this.data = JSON.stringify(payload)
      },
      cookie(name, value, options) {
          this.headers[name] = value
      }
    }

    next.mockReset()
  })

  test('should correctly set cookie', async () => {
    const expectedToken = 'expected'
    res.query.token = ''

    tokenRedirect(req, res, next)

    expect(res.headers.token).toBeDefined()
    expect(res.headers.token).toEqual(expectedToken)
  })
})
Run Code Online (Sandbox Code Playgroud)