简体   繁体   中英

Test: Using self, expected: 1 time with arguments received: 0 times

I'm currently working on writting test for a Proxy Class that uses Boxr gem to make http calls to Box API. Currently, I have the following method:

class BoxApi
 ####
  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

I want to test that when upload_file_to_box is executed, the client 's method upload_file is called with arguments file_path and folder . I guess the error in this situation is the self.folder method.

Which retrieves a folder from it's id as follows:

class BoxApi
#######
  def folder(box_folder_id)
    prepare_folder(box_folder_id)
  end

  def prepare_folder(folder_id)
    client.folder_from_id(folder_id)
  end

I'm thinking this is where the error is coming from. However, the method is stubbed to return a string. allow(subject).to receive(:folder).with(folder_id).and_return('123')

Here is the full method test:

context 'success' do
  subject { BoxApi.new }
  let(:client) { Boxr::Client.new(refresh_token: nil, jwt_private_key: nil) }

  let(:file_path) {'spec/fixtures/pdfs/pdf_without_data.pdf'}
  let(:file_name) {'pdf_without_data.pdf'}
  let(:folder_id) {'123'}

  it 'Uploads file to folder on Box' do
    allow(subject).to receive(:client).and_return(client)
    allow(subject).to receive(:folder).with(folder_id).and_return('123')
    allow(client).to receive(:upload_file).with(file_path, folder_id).and_return(status: 201)


    subject.upload_file_to_box(file_path, file_name, folder_id)

    expect(client).to receive(:upload_file).with(file_path, folder_id)
  end
end

If I pry into the test case, I'm able to see that calling folder does return the expect string.

[1] pry(#<RSpec::ExampleGroups::BoxApi::UploadFileToBox::Fail>)> subject.upload_file_to_box(file_path, file_name, folder_id)
=> {:status=>201}
[2] pry(#<RSpec::ExampleGroups::BoxApi::UploadFileToBox::Fail>)> self
=> #<RSpec::ExampleGroups::BoxApi::UploadFileToBox::Fail "Uploads file to folder on Box" (./spec/clients/box_api.rb:101)>
[3] pry(#<RSpec::ExampleGroups::BoxApi::UploadFileToBox::Fail>)> subject.folder(folder_id)
=> "123"
[4] pry(#<RSpec::ExampleGroups::BoxApi::UploadFileToBox::Fail>)>

however, when I run the test I still get the error as if upload_file_to_box is never called.

Failures:

  1) BoxApi#upload_file_to_box fail Uploads file to folder on Box
     Failure/Error: expect(client).to receive(:upload_file).with(file_path, folder_id)

       (#<Boxr::Client:0x00007fea5f8b1db0 @access_token="123", @refresh_token=nil, @client_id="1234", @client_secret="345", @enterprise_id=nil, @jwt_private_key=nil, @jwt_private_key_password="1234545677", @jwt_public_key_id="12345467", @identifier=nil, @as_user_id=nil, @token_refresh_listener=nil>).upload_file("spec/fixtures/pdfs/pdf_without_data.pdf", "123")
           expected: 1 time with arguments: ("spec/fixtures/pdfs/pdf_without_data.pdf", "123")
           received: 0 times

我认为您可以首先将expect(client).to receive(:upload_file).with(file_path, folder_id)放在subject.upload_file_to_box(file_path, file_name, folder_id)之上

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM