有没有办法用Jasmine NodeJS发送JPG?

我试图用FrisbyJS(它位于Jasmine for Node的顶部)testing我的API。 我想知道是否有人知道如何使用茉莉花提交/发送图像?

http://frisbyjs.com/

我目前的代码是…

var frisby = require('frisby'); var URL = 'http://localhost/api'; frisby.globalSetup({ request: { headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json', 'api': 'key' } }, timeout: (30 * 1000) }); frisby.create('submit an image') .post(URL + 'image', {images: '@test.jpg'}, {method: 'POST'}) .expectStatus(200) .afterJSON(function(cart){ console.log('made it!'); }) }).toss(); 

我得到以下错误:

  1) Frisby Test: submit an image [ POST http://localhost/api ] Message: timeout: timed out after 30000 msec waiting for HTTP Request timed out before completing Stacktrace: undefined Finished in 31.458 seconds 

是的,图像test.jpg确实存在于相同的文件夹:)作为规格文件,我在哪里执行规格本身(茉莉花节点)。

我在https://github.com/vlucas/frisby/blob/master/examples/httpbin_multipart_spec.js上find了一个解决scheme

 var frisby = require('frisby'); var fs = require('fs'); var path = require('path'); var FormData = require('form-data'); var contentPath = path.resolve(__dirname, './path/to/image.jpg'); var form = new FormData(); form.append('file', fs.createReadStream(contentPath), { knownLength: fs.statSync(contentPath).size }); frisby.create('Create content asset') .post(apiPath + '/assets', form, { json: false, headers: { 'content-type': 'multipart/form-data; boundary=' + form.getBoundary(), 'content-length': form.getLengthSync(), } }) .expectStatus(201) .toss()