javascript - 使用超级测试传递请求正文以发布
问题描述
我正在使用 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.
解决方案
你的问题是你对那些模拟函数在做什么有错误的理解。首先,您完全覆盖了AValidator
和BValidator
的原始值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();
})
推荐阅读
- go - 如何限制字符串标志中可接受的值范围?
- spring-data-jpa - Spring @TransactionalEventListener 的集中式错误处理程序
- reactjs - 如何在部署在 Google 云上的使用 npx 命令创建的反应应用程序中启用 https。需要使用从 Godaddy 购买的证书
- swift - 从某个点 Swift 开始视频
- javascript - 最简单的 Javascript Monad 实现
- python - 套接字代码 末尾的 Python 代码影响开头的代码
- javascript - 如果日期匹配,则将数组对象过滤到其他数组对象
- javascript - 同级对象的组数组
- c# - 在我的 POST 请求操作中,JsonProperty 映射不起作用
- php - PhpWord 有没有办法在 PHP 中插入特定代码以在 word 文档中自动生成今天的日期?