使用超级测试传递请求正文以进行发布

Wol*_*lzy 11 javascript testing supertest jestjs

我正在使用超级测试测试快速服务器,并且我需要测试后调用。我认为帖子应该成功并返回状态 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"
    });
});
Run Code Online (Sandbox Code Playgroud)

这是 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
            });

        }
    }
}

Run Code Online (Sandbox Code Playgroud)

这是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.
Run Code Online (Sandbox Code Playgroud)

小智 21

我假设您已正确设置解析器。例子:

 const app = express();
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({extended: true}));
Run Code Online (Sandbox Code Playgroud)

一旦设置完毕,我相信您会丢失标题。例子:

const payload = {name: 'john', email: 'xyz@sadfjak.com', password: '2342388' };
const res = await request(app)
            .post('/api/register')
            .send(payload)
            .set('Content-Type', 'application/json')
            .set('Accept', 'application/json')
Run Code Online (Sandbox Code Playgroud)


Isa*_*ine -3

您的问题是您对这些模拟函数的作用有错误的理解。首先,您完全覆盖了AValidatorBValidator的原始值jest.fn()

这样做

let AValidator = require('../src/AValidator');
let BValidator = require('../src/BValidator');
Run Code Online (Sandbox Code Playgroud)

在你的测试中是多余的。

的目的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());
Run Code Online (Sandbox Code Playgroud)

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

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

supertest你的要求应该看起来像

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