Google-api-ruby-client: Create file returns nil when using WebMock gem to stub_request

Created on 9 May 2017  路  2Comments  路  Source: googleapis/google-api-ruby-client

Hi there!

I'm testing some functionality of my application that will store file Id's to update them at some point in the future. The app works fine however I'm having some issues with the tests.

This is my stub

    stub_request(:post, "https://www.googleapis.com/upload/drive/v3/files").
         to_return(status: 200, body: '{"kind": "drive#file","id": "123456", "name": "File Name", "mimeType": "application/vnd.google-apps.document"}', headers: {"Content-Type"=> "application/json"})

However on my code, when I do response.id on the tests it says the following:

RuntimeError: Additional error (RuntimeError: Additional error (NoMethodError: undefined method `id' for nil:NilClass

The code works fine as the app does what I want it to do, also the stubs work for other libraries/parts of the application. I just can't figure out how to make it work with this library so when I stub that request I get the proper file based on that mocked data.

Success - #<Google::Apis::DriveV3::File:0x007f984a625590
 @id="123456",
 @kind="drive#file",
 @mime_type="application/vnd.google-apps.document",
 @name="File Name">

Thanks!

question

Most helpful comment

Can you provide a more detailed stack trace of the error?

You may also want to try adding the response header "X-Goog-Upload-Status: final". Since the client uses the resumable upload protocol which typically involves 2+ HTTP requests, it's not interpreting the response correctly. Setting that header should direct the client to interpret the mocked response as the final response in the upload process.

All 2 comments

Can you provide a more detailed stack trace of the error?

You may also want to try adding the response header "X-Goog-Upload-Status: final". Since the client uses the resumable upload protocol which typically involves 2+ HTTP requests, it's not interpreting the response correctly. Setting that header should direct the client to interpret the mocked response as the final response in the upload process.

Hey @sqrrrl ! Thanks for your quick reply. The backtrace doesn't help much as the library itself is not failing, it was just returning null and the error was on my side so I couldn't really figure out what was wrong.

Adding the header X-Goog-Upload-Status: final did the trick. Thank you very much! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

majioa picture majioa  路  6Comments

arjunmenon picture arjunmenon  路  5Comments

ProbablyRusty picture ProbablyRusty  路  5Comments

jponc picture jponc  路  5Comments

gimite picture gimite  路  3Comments