如何使用Supertest附加多个表单数据字段?

问题描述

我正在执行集成测试,在这种情况下,我必须在表单数据中提供字段。我可以在.attach提供一个密钥和文件。就我而言,我必须在同一请求中的表单数据中提供五个键和值。如何实现?

enter image description here

我已经执行了示例代码

      const response = await request(server)
        .post('url for the request')
        .set({
          'Content-Type': 'application/json',})
        .attach('key','file');

解决方法

您可以使用.field()添加其他基于文本的字段(有关更多详细信息,请参见docs

  const response = await request(server)
        .post('url for the request')
        .set({
          'Content-Type': 'application/json',})
        .field('category','Product')
        .field('type','Image')
        .field('category','Product')
        .field('entity_no','12354')
        .attach('file','/path/to/file');

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...