首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fixture_file_upload的参数在请求规范中转到String类。

fixture_file_upload的参数在请求规范中转到String类。
EN

Stack Overflow用户
提问于 2014-09-22 08:41:39
回答 2查看 994关注 0票数 0

下面的代码会产生一些错误。

规范/rails_helper.rb

代码语言:javascript
复制
RSpec.configure do |config|
  config.include ActionDispatch::TestProcess

  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  ...
end

spec/请求/api/v1/映像_spec.rb

代码语言:javascript
复制
require 'rails_helper'

describe 'Images', type: :request do
  describe 'POST api/v1/images' do
    it 'should have status :created' do
      post api_v1_images_path(content: fixture_file_upload('test.jpg', 'image/jpeg'), position: 0), nil , nil
      # #<Rack::Test::UploadedFile:0x007f92beb9cfc8 @content_type={:content_type=>"image/jpeg"}, @original_filename="test.jpg", @tempfile=#<Tempfile:/var/folders/qv/8k9_kbs52w5ddhb1ph41xngh0000gn/T/test.jpg20140922-11520-1co5l19>>

      expect(response).to have_http_status(:created)
    end
  end
end

app/controllers/api/v1/映像_Controller.rb

代码语言:javascript
复制
def create
  puts params[:content].class #=> String
  # String..!? What's going on..!?
  #
  # I'd like to pass upload rspec with carrierwave.
  # Creating model with uploader by params[:content] will cause null value of image attributes of model.
end

对象id是相同的,但是属性消失了。发生什么事了?

Gemfile.lock

代码语言:javascript
复制
rails (4.1.5)
  actionmailer (= 4.1.5)
  actionpack (= 4.1.5)
  actionview (= 4.1.5)
  activemodel (= 4.1.5)
  activerecord (= 4.1.5)
  activesupport (= 4.1.5)
  bundler (>= 1.3.0, < 2.0)
  railties (= 4.1.5)
  sprockets-rails (~> 2.0)
rspec (3.1.0)
  rspec-core (~> 3.1.0)
  rspec-expectations (~> 3.1.0)
  rspec-mocks (~> 3.1.0)
rspec-core (3.1.0)
  rspec-support (~> 3.1.0)
rspec-expectations (3.1.0)
  diff-lcs (>= 1.2.0, < 2.0)
  rspec-support (~> 3.1.0)
rspec-mocks (3.1.0)
  rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
  actionpack (>= 3.0)
  activesupport (>= 3.0)
  railties (>= 3.0)
  rspec-core (~> 3.1.0)
  rspec-expectations (~> 3.1.0)
  rspec-mocks (~> 3.1.0)
  rspec-support (~> 3.1.0)
rspec-support (3.1.0)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-02 07:46:13

我滥用了邮政法..。

以前:post api_v1_images_path(content: fixture_file_upload('test.jpg', 'image/jpeg'), position: 0), nil , nil

后:post api_v1_images_path, {content: fixture_file_upload('test.jpg', 'image/jpeg'), position: 0}, nil

“后”通过了规范。

票数 0
EN

Stack Overflow用户

发布于 2017-06-01 20:10:38

Rails 5:

在此之前:

post api_v1_images_path params: { content: fixture_file_upload('test.jpg', 'image/jpeg'), position: 0 }

之后:

post api_v1_images_path {}, params: { content: fixture_file_upload('test.jpg', 'image/jpeg'), position: 0 }

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25969856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档