如何在 mocha 中使用嵌套的 json 数据测试图像上传

问题描述

这是我的测试片段。我需要使用嵌套 json 和图像传递附加数据。

`

const email = 'test.email@gmail.com';
const contact = {
    firstName: 'John',lastName: 'Doe',phone: '9876543212'
  };

const response = await chai
    .request(server)
    .post('/profile')
    .set('Content-Type','multipart/form-data')
    .attach('logo','test/test-images/logo.png')
    .field({
      email,name: 'All Tech Solutions',phone: '9812345678',contact: contact
    });

  expect(response.body.status).to.equal('created');
  expect(response.body.profile.email).to.equal(email);
  expect(response.body.profile.logo).to.exist;
});

`

它适用于没有嵌套 json 的参数,但不适用于嵌套 json。如何传递带有徽标的嵌套 json?

解决方法

看看 .field() 方法签名:

field(name: string,val: MultipartValue): this;
field(fields: { [fieldName: string]: MultipartValue }): this;

MultipartValue 类型:

type MultipartValueSingle = Blob | Buffer | fs.ReadStream | string | boolean | number;

type MultipartValue = MultipartValueSingle | MultipartValueSingle[];

该字段的值应为 Blob | Buffer | fs.ReadStream | string | boolean | number;。它不接受对象。

所以你需要使用 JSON.stringify(contact)

例如

app.js

const express = require('express');
const multer = require('multer');
const path = require('path');
const upload = multer({ dest: path.resolve(__dirname,'uploads/') });

const app = express();

app.post('/profile',upload.single('logo'),(req,res) => {
  console.log(req.body);
  console.log(req.file);
  const profile = { email: req.body.email,logo: true };
  res.json({ status: 'created',profile });
});

module.exports = app;

app.test.js

const server = require('./app');
const chai = require('chai');
const path = require('path');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const expect = chai.expect;

describe('66273169',() => {
  it('should pass',async () => {
    const email = 'test.email@gmail.com';
    const contact = {
      firstName: 'John',lastName: 'Doe',phone: '9876543212',};

    const response = await chai
      .request(server)
      .post('/profile')
      .set('Content-Type','multipart/form-data')
      .attach('logo',path.resolve(__dirname,'logo.png'))
      .field({
        email,name: 'All Tech Solutions',phone: '9812345678',contact: JSON.stringify(contact),});

    expect(response.body.status).to.equal('created');
    expect(response.body.profile.email).to.equal(email);
    expect(response.body.profile.logo).to.exist;
  });
});

测试结果:

  66273169
[Object: null prototype] {
  email: 'test.email@gmail.com',contact: '{"firstName":"John","lastName":"Doe","phone":"9876543212"}'
}
{
  fieldname: 'logo',originalname: 'logo.png',encoding: '7bit',mimetype: 'image/png',destination: '/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66273169/uploads',filename: '7b82676df1678c79da43abcfbd9d411f',path: '/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66273169/uploads/7b82676df1678c79da43abcfbd9d411f',size: 0
}
    ✓ should pass


  1 passing (31ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 app.js   |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

相关问答

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