簡體   English   中英

如何使用 SuperTest 進行單元測試以使用 multer 發布文件

[英]How to unit test with SuperTest to post files with multer

現在,我有一個表單,它使用 multer 和 aws-S3 提交圖像“文件”數組,並接收以下類型的 object 以在 POST controller 中進行處理:

files: [
{
  fieldname: 'uploaded',
  originalname: 'test.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  size: 1407923,
  bucket: 'mybucket',
  key: '2343.jpg',
  acl: 'public-read',
  contentType: 'application/octet-stream',
  contentDisposition: null,
  storageClass: 'STANDARD',
  serverSideEncryption: null,
  metadata: null,
  location: 'https://mybucket.s3.us-east-2.amazonaws.com/12144etest.jpg',
  etag: '"7503d8c8f9cdca"',
  versionId: undefined
}

]

如何使用 supertest 發布這樣的模擬 object? 現在我有這個,但我收到一個 500 內部服務器錯誤,很明顯我做錯了什么:

let res = await api
  .post("/login")
  .send({"username" : "user", "password": "pass"})

await supertest(app)
  .post('/upload')
  .attach('files', 'test.jpg')
  .set('cookie', res.headers['set-cookie'])
 

使用文件的完整路徑將解決您的問題。

await supertest(app) .post('/upload') .attach('files', `${__dirname}/test.jpg`) // added __dirname here.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM