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

时间:2014-09-22 08:41:40

标签: ruby-on-rails ruby rspec

以下代码会产生一些错误。

规格/ rails_helper.rb

RSpec.configure do |config|
  config.include ActionDispatch::TestProcess

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

  ...
end

规格/请求/ API / V1 / images_spec.rb

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

应用程序/控制器/ API / V1 / images_controller.rb

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的

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)

2 个答案:

答案 0 :(得分:0)

我误用了post方法......

在: 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

&#34;后&#34;通过了规范。

答案 1 :(得分:0)

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 }