I'm currently working on a Ruby application using Boxr gem. The gem is use to interact with Box API. I created a Proxy class to make calls to Box API using the Boxr gem.
I'm testing uploading a file to box. I created a method upload_file_to_box which pushes a file to a folder on Box. The method is written as follows:
def upload_file_to_box(file_path, file, box_folder_id)
client.upload_file(file_path, self.folder(box_folder_id))
rescue Boxr::BoxrError
file = find_file_by_name(file).shift
client.upload_new_version_of_file(file_path, file)
end
The stub I created using Webmock
stub_request(:post, "https://upload.box.com/api/2.0/files/content").
to_return(:status => 200, :body => {message: "File Uploaded"}.to_json, :headers => {})
However when I run the test I get the status and message as an error:
37: def upload_file_to_box(file_path, file, box_folder_id)
38: client.upload_file(file_path, self.folder(box_folder_id))
39: rescue Boxr::BoxrError
=> 40: binding.pry
41: file = find_file_by_name(file).shift
42: client.upload_new_version_of_file(file_path, file)
43: end
[1] pry(#<BoxApi>)> client.upload_file(file_path, self.folder(box_folder_id))
Boxr::BoxrError: 200: File Uploaded
from /Users/stevenaguilar/.rvm/gems/ruby-2.5.3/gems/boxr-1.4.0/lib/boxr/client.rb:239:in `check_response_status'
Caused by Boxr::BoxrError: 200: File Uploaded
from /Users/stevenaguilar/.rvm/gems/ruby-2.5.3/gems/boxr-1.4.0/lib/boxr/client.rb:239:in `check_response_status'
[2] pry(#<BoxApi>)>
Running client.upload_file(file_path, self.folder(box_folder_id)) returns the correct status and body. However the test don't pass and is shown as an error: 
Shouldn't result be set to equals {message: "File Uploaded"} ?
Here is the full test.
describe '#upload_file_to_box' do
context 'success' do
it 'Uploads file to folder on Box' do
stub_request(:options, "https://api.box.com/2.0/files/content").
with(:body => {"{\"name\":\"pdf_without_data.pdf\",\"parent\":{\"id\":\"123\"},\"size\":64197}"=>nil}).
to_return(:status => 200, :body => {message: 'PDF content added'}.to_json, :headers => {})
stub_request(:get, "https://api.box.com/2.0/folders/123").
to_return(:status => 200, :body => {id: "123"}.to_json, :headers => {})
stub_request(:post, "https://upload.box.com/api/2.0/files/content").
to_return(:status => 200, :body => {message: "File Uploaded"}.to_json, :headers => {})
stub_request(:get, "https://api.box.com/2.0/search?limit=30&offset=0&query=pdf_without_data.pdf").
to_return(:status => 200, :body => {id: '123', type: 'file'}.to_json, :headers => {})
allow_any_instance_of(BoxApi).to receive(:client)
.and_return(Boxr::Client.new(refresh_token: nil, jwt_private_key: nil))
box_api = BoxApi.new
file_path = 'spec/fixtures/pdfs/pdf_without_data.pdf'
file_name = 'pdf_without_data.pdf'
folder_id = '123'
result = box_api.upload_file_to_box(file_path, file_name, folder_id)
binding.pry
expect { result['message'] }.to eq("success")
end
end
end
Aucun commentaire:
Enregistrer un commentaire