How to mock aws-sdk gem?

前端 未结 2 1358
迷失自我
迷失自我 2021-02-08 15:20

I have some code that uploads a file to Amazon S3, using the aws-sdk gem. Apparently it does an HTTP put to upload the file.

Is there a good way to mock thi

2条回答
  •  再見小時候
    2021-02-08 16:06

    If you're using version 2 of the aws-sdk gem try adding:

    Aws.config.update(stub_responses: true)
    

    to your RSpec.configure block (usually found in your rails_helper.rb file)


    While the above works, it will return empty responses if you don't further specify response content - not necessarily valid, but stubbed.

    You can generate and return stubbed response data from a named operation:

    s3 = Aws::S3::Client.new
    s3.stub_data(:list_buckets)
    #=> #>
    

    In addition to generating default stubs, you can provide data to apply to the response stub.

    s3.stub_data(:list_buckets, buckets:[{name:'aws-sdk'}])
    #=> #], owner=#>
    

    For more details refer to: http://docs.aws.amazon.com/sdkforruby/api/Aws/ClientStubs.html

提交回复
热议问题