【问题标题】:RSpec Post with a file, params and header field带有文件、参数和标题字段的 RSpec Post
【发布时间】:2015-10-06 12:35:39
【问题描述】:

我正在尝试使用 RSpec 来测试一个用于上传文件的 api。 api 请求是一个简单的 POST 请求,其中包含一个文件以及一些参数和一个标头字段。

我尝试使用以下代码发出 POST 请求:

post "media.upload", { client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json' },
{ 'HTTP_PROXY_SSL' => 'true' }, :upload => @file

但我收到以下错误:

Failure/Error: post "cm/media.upload", { client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json' },
     ArgumentError:
       wrong number of arguments (4 for 1..3)

@file 变量定义为:

@file = fixture_file_upload(Rails.root + 'spec/fixtures/images/abc.jpg', 'image/jpeg')

所以我的问题是如何在 RSpec 中使用文件、参数和一些标头进行 POST 请求?

【问题讨论】:

    标签: ruby-on-rails ruby rspec bdd rspec-rails


    【解决方案1】:

    upload 参数与其余请求参数一起移入:

    params = {upload: @file, client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json'}
    headers = {'HTTP_PROXY_SSL' => 'true'}
    
    post 'media.upload', params, headers
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多