bekkou68
bekkou68

Reputation: 852

parameter of fixture_file_upload went to String class in request spec

The following code produces some wrongs.

spec/rails_helper.rb

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

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

  ...
end

spec/request/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

app/controllers/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

Object id is same, but attributes are disappeared.. What's happen?

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)

Upvotes: 0

Views: 1229

Answers (2)

coderberry
coderberry

Reputation: 3160

Rails 5:

Before:

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

After:

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

Upvotes: 2

bekkou68
bekkou68

Reputation: 852

I misused post method...

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

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

"After" passed spec.

Upvotes: 0

Related Questions