首页 > 解决方案 > 使用超级测试传递请求正文以发布

问题描述

我正在使用 super-test 测试快速服务器,我需要测试一个 post call。我假设帖子应该成功并返回 200 的状态,但它返回 401。有人告诉我,我需要通过帖子传递请求正文,但我不确定如何执行此操作。

我尝试使用 .send({name: 'aName'}) 但这给了我相同的 401 代码。

下面是app.js

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const hateoasLinker = require('express-hateoas-links');
const AValidator = require('./AValidator');
const BValidator = require('./BValidator');
const schema_v1 = require("./schema.json");
const {
    logService: logger
} = require("@utils");

let aValidator = AValidator(schema_v1);

let ValidatorApi = BValidator.ValidatorApi('api');
let adminValidator = BValidator.ValidatorAdmin('admin');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(hateoasLinker);

app.post('/*/activate',admiValidator, (req, res) => {
    console.log("In Activate===============>");
    res.status(200);
    res.json({
        rel: "self",
        method: "POST",
        title: 'Activate Solution',
        href: "/activate"
    });
});

这是 BValidator 的代码

ValidatorAdmin = function(callType){
    return function (req,res,next){
        let authoizationHeader = req.headers['authorization'];
        try {
        Verifier.verifyPayload(authoizationHeader, callType, (verificationError) => {
            if (verificationError) {
                res.setHeader('Content-Type', 'application/json');
                res.status(401);
                res.json({
                    message : "verificationError "+verificationError.message
                });
            } else {
                next();
            }
        });
        } catch (authorizationError) {
            res.setHeader('Content-Type', 'application/json');
            res.status(401);
            res.json({
                message : authorizationError.message
            });

        }
    }
}

这是 app.test.js

const request = require('supertest');
const bodyParser = require('body-parser');
let AValidator = require('../src/AValidator');
let BValidator = require('../src/BValidator');
BValidator = jest.fn();
AValidator = jest.fn();
app = require('../src/app');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

describe('Test os GET/POST calls in app.js', ()=>{

  test('Tests activate post', (done)=>{

    BValidator.mockReturnValue({
      ValidatorApi: (req,res,next)=>{
        next();
      },
      ValidatorAdmin:(req,res,next)=>{
        next();
      }
    });

    AValidator.mockImplementation((schema)=>{
      return function (req,res,next){
        next();
      }
    });


    request(app)
      .post('/test/activate')
      .set({name:'josh'})
      .then((response)=>{
        expect(response.statusCode).toBe(200);
        done();
      })

  })


});

So ultimately I'd like this post to resolve successfully and return a status code of 200.

标签: javascripttestingjestjssupertest

解决方案


你的问题是你对那些模拟函数在做什么有错误的理解。首先,您完全覆盖了AValidatorBValidator的原始值jest.fn()

这样做

let AValidator = require('../src/AValidator');
let BValidator = require('../src/BValidator');

在您的测试中是多余的。

的目的mockReturnValue是让您可以调用该函数并取回您指定的返回值。

直接取自 Jest 文档

const myMockFn = jest
  .fn()
  .mockReturnValue('default')
  .mockReturnValueOnce('first call')
  .mockReturnValueOnce('second call');

// 'first call', 'second call', 'default', 'default'
console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());

您永远不会使用或调用您的模拟函数,而且您的 api 甚至不知道它们是否存在。

解决方案是在运行测试时在请求中提供适当的标头,这样它们就不会在中间件中失败。此外,要做到这一点,你必须知道Verifier.verifyPayload在做什么。

你的supertest要求应该看起来像

request(app)
.post('/test/activate')
.set({authorization: 'a_valid_value_goes_here'})
.then((response)=>{
   expect(response.statusCode).toBe(200);
   done();
})

推荐阅读