1

I create an api in my rails application with grape.

Now i want to test all of my api. In my api, i can upload sound, get sound/:id and delete sound/:id.

I start to write test for post sound but my test all time return "undefined method [] for nil:NilClass".

My curl command for upload ( that's work ) :

curl -X POST -i -F data=@SOUND/test.mp3 "http://localhost:3000/api/sounds/new?access_token=1234567"

I think the problem is in curl when you use "-F data=", you need to add @ after the "=" if I remove the @ , i have the undefined method [] for nil:NilClass error.

Now my question is how i can add in my test the @ for test upload file ?

My test :

it "uploads a file" do
    sound_filename = "spec/fixtures/test.mp3"
    post "/api/sounds/new", data= Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)
    expect(response.status).to eq(201)
    expect(response.headers['Content-Type']).to eq("audio/mp3")
    expect(response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''test.mp3")
  end

This test return this error : undefined method [] for nil:NilClass error . Anyone know how i can fix it ?

Thank's for your help.

1 Answers1

1

How about replace this line

post "/api/sounds/new", data= Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)

with

post "/api/sounds/new", {data: Rack::Test::UploadedFile.new(sound_filename, 'audio/mp3', true)}