我正在尝试编写一个测试,检查文件上传是否已正确处理。
这是控制器:
module.exports = {
friendlyName: 'Submit a file',
inputs: {
filesToUpload: {
type: 'ref',
example: 'file1',
},
},
files: ['filesToUpload'],
exits: {
success: {
statusCode: 200,
description: 'New file was uploaded successfully.'
},
},
fn: async function (inputs) {
inputs.filesToUpload.upload({
maxBytes: 22000000
}, async (err, files) => {
await File.create({
name: files[0].filename,
path: files[0].fd
})
return {}
})
}
};
测试:
it('should upload a new file', (done) => {
testSession
.post('/files/upload')
.attach('filesToUpload', './assets/images/logo2.png')
.expect(200)
.end(async (err) => {
if (err) {
return done(err)
} else {
return done()
}
})
}
和bootstrap.js文件
let sails = require('sails')
let _ = require('lodash')
global.chai = require('chai')
global.should = chai.should()
before(function (done) {
this.timeout(15000)
sails.lift({
log: {
level: 'warn'
},
hooks: {
grunt: false
},
security: {
csrf: false
},
datastores: {
default: {
adapter: 'sails-disk',
inMemoryOnly: true
},
},
}, async (err) => {
if (err) return done(err)
done(err, sails)
})
})
after((done) => {
if (sails && _.isFunction(sails.lower)) {
sails.lower(done)
}
})
但是,出现以下错误
(node:19912) UnhandledPromiseRejectionWarning: AdapterError: Unexpected error from database adapter: Unrecognized datastor
e: `default`, It doesn't seem to have been registered with this adapter (sails-disk).
at inputs.filesToUpload.upload (\api\controllers\files\upload.js:45:22)
at cb (\node_modules\skipper\lib\private\Upstream\prototype.upload.js:80:14)
at Writable.allFilesUploaded (\node_modules\skipper\lib\private\Upstream\prototype.upload.js:1
41:5)
at Object.onceWrapper (events.js:277:13)
at Writable.emit (events.js:194:15)
at finishMaybe (_stream_writable.js:641:14)
at afterWrite (_stream_writable.js:481:3)
at onwrite (_stream_writable.js:471:7)
at WriteStream.successfullyWroteFile (\node_modules\skipper-disk\standalone\build-disk-receive
r-stream.js:138:9)
at WriteStream.emit (events.js:194:15)
at finishMaybe (_stream_writable.js:641:14)
at stream._final (_stream_writable.js:619:5)
at WriteStream._final (internal/fs/streams.js:263:3)
at callFinal (_stream_writable.js:612:10)
at process._tickCallback (internal/process/next_tick.js:63:19)
在调试模式下,似乎在await Document.create
行上失败。奇怪的是,所有其他不包含文件上载的控制器都已成功测试,即,如果行await Document.create
位于inputs.filesToUpload.upload
之外,则不会引发错误。