AdonisJS 5中POST请求测试咨询:原send参数方式失效
AdonisJS 5 测试POST请求的正确方式
在AdonisJS 5中,测试客户端的API已更新,不再沿用AdonisJS 4里的.send()方法传递请求体参数,可根据接口接收的数据类型选择以下方式:
1. 发送JSON格式请求体
若接口接收JSON数据,使用.json()方法传递参数:
client.post('posts').json({ title: 'Adonis 101', body: 'Post content' })
2. 发送表单格式请求体
若接口接收application/x-www-form-urlencoded类型的表单数据,使用.form()方法:
client.post('posts').form({ title: 'Adonis 101', body: 'Post content' })
完整测试示例
在单元测试文件中可按如下方式编写:
import { test } from '@japa/runner' test('创建新文章', async ({ client }) => { const response = await client.post('posts').json({ title: 'Adonis 101', body: 'Post content' }) response.assertStatus(201) response.assertBodyContains({ title: 'Adonis 101' }) })
内容的提问来源于stack exchange,提问作者Andrés Felipe Echavarría
相关产品推荐
相关产品推荐

