aws-sdk-ruby のスタブ機能がgoogle-cloud-rubyにあったら嬉しい
タイトルが全部。
皆さんご存知、AWSの公式API client libraryである aws-sdk-ruby ですが、API callに対するstub機能が組込まれているので、テストを書くとき非常に助かります。
documentから引用するのですが、このようにresponseをstubすることができます。
# stub data in the constructor
client = Aws::S3::Client.new(stub_responses: {
list_buckets: { buckets: [{name: 'my-bucket' }] },
get_object: { body: 'data' },
})
client.list_buckets.buckets.map(&:name) #=> ['my-bucket']
client.get_object(bucket:'name', key:'key').body.read #=> 'data'
さて、Google Cloud Platform公式のAPI client libraryである google-cloud-ruby ですが、そのような機能はありません。
そういう機能はないのか?というissueが立てられたこともありますが、
I've always created an extra Google Cloud project and used the live service.
というコメント(Google内部の人ではありませんが)がされており、特に対応はないままself closeされています。
生きたserviceでtestをするということに対するメリットがあるのはわかりますが、stubが公式でサポートされていてくれたら便利なので、欲しいですね。
ちなみに僕は次のようなmock codeを書いて、Google::Cloud::StorageへのAPI callを隠蔽しています。
client = double('client double')
bucket = double('bucket object double')
gcp_file = double('gcp file object double')
file = double('file object double')
allow(file).to receive(:read).and_return(File.read(Rails.root.join('spec', 'resources', '1px.png')))
allow(gcp_file).to receive(:download).and_return(file)
allow(bucket).to receive(:create_file)
allow(bucket).to receive(:file).and_return(gcp_file)
allow(client).to receive(:bucket).and_return(bucket)
allow(Google::Cloud::Storage).to receive(:new).and_return(client)
aws-sdk-rubyが恋しい夜もある
— うなすけ (@yu_suke1994) May 15, 2019
いいなと思ったら応援しよう!
