我正在尝试使用Mocha / Chai测试我的nodejs API。我创建了一个"测试"包含File.js的文件夹,其中包含以下代码:
let chai = require('chai');
let chaiHttp = require('chai-http');
let fs = require('fs');
let should = chai.should();
chai.use(chaiHttp);
var fileName = 'devJoke.png';
describe('/PUT file', () => {
it('Should PUT a file', (done) => {
let file = {
fileName: "devJoke.png",
}
chai.request('http://localhost:3000')
.put('/bucket/user2/files/')
.set('secret_key', 'user2SecretKey')
.set('Content-Type', 'application/x-www-form-urlencoded')
.field('Content-Type', 'multipart/form-data')
.field('fileName', 'devJoke.png')
.attach('files', "/Users/stagiaire/Downloads/devJoke.png")
.send(file)
.end((err, res) => {
if (err) {
console.log(err)
} else {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('data').eql('File Uploaded')
}
done();
})
})
})
但是当我在执行命令npm test时,我收到了错误
{
Error: read ECONNRESET
at _errnoException (util.js:1019:11)
at TCP.onread (net.js:608:25)
code: 'ECONNRESET',
errno: 'ECONNRESET',
syscall: 'read',
response: undefined }
使用PostMan我使用标题:
{"Content-Type":"application/x-www-form-urlencoded"}
{"secret_key":"user2SecretKey"}
在身上得到了这个:
{"fileName":"devJoke.png"}
{"files": myFile}
它与PostMan合作,我尝试用mocha / chai做同样的事情。我做错了什么?