S3 ã使ã£ãå®è£ ã®ãã¹ãã fake-s3 ã使ã£ã¦æ¸ãã¦ã¿ã
fakes3 (fake-s3) ã¨ãã gem ã使ãã¨ãS3 ã®ããã¼ãµã¼ã(webrick)ã使ããããã«ãªãã¨ã®ãã¨ã
- fakes3 㨠fakefs 㧠Amazon S3 é£æºã®ãã¹ããæ¸ãã¦ã¿ã - ããã³ãæ¥è¨
- jubos/fake-s3
ãµã¼ãããã»ã¹ã®ç®¡çãã©ãããã®ãä»ã£ã½ãã®ãåããããã°ã°ã£ã¦å¾ãããæ å ±ãå ã«ãglint ã¨ãã gem ã使ã£ã¦ã¿ããã¨ã«ã
before(:suite)
㧠glint ã« fakes3 ã®é¢åãè¦ã¦ãããã
after(:suite)
ã¯ãfakes3 ã«æ¸¡ããä¸æãã£ã¬ã¯ããªã®å¾å§æ«ãè¡ãã®ã¿ã
# spec/support/fakes3.rb require 'tmpdir' require 'glint' RSpec.configure do |config| config.before :suite do rootdir = Dir.mktmpdir server = Glint::Server.new(nil, signals: [:INT]) do |port| exec "bundle exec fakes3 -p #{port} -r #{rootdir}", err: '/dev/null' exit 0 end server.start Glint::Server.info[:fakes3] = { address: "127.0.0.1:#{server.port}", root: rootdir } end config.after :suite do if Dir.exists? Glint::Server.info[:fakes3][:root] FileUtils.remove_entry_secure(Glint::Server.info[:fakes3][:root]) end end end
ã¤ãã§ã«ããã¹ãå®è¡ä¸ã«ä¸ãä¸èª¤ã£ã¦æ¬å½ã® S3 ã«ç¹ãããªãæ§ã« webmock ãå ¥ãã¦ã¿ãã
# spec/support/webmock.rb require 'webmock/rspec' WebMock.disable_net_connect!(:allow_localhost => true)
é©å½ã« example ãæ¸ãã¦åä½ç¢ºèªãã¦ã¿ããã©ãæ¬å½ã«ãããªæãã§è¯ãã®ãã¯ããåãã£ã¦ãªãã
# spec/lib/example_spec.rb require 'spec_helper' require 'aws-sdk' RSpec.describe Aws::S3 do let(:s3_client) do Aws::S3::Client.new( :access_key_id => 'YOUR_ACCESS_KEY_ID', :secret_access_key => 'YOUR_SECRET_ACCESS_KEY', :region => 'ap-northeast-1', :endpoint => "http://#{Glint::Server.info[:fakes3][:address]}/", :force_path_style => true) end before do Aws::S3::Resource.new(client: s3_client) .create_bucket(bucket: 'mybucket') .put_object(key: 'myobject', body: 'this is a pen.') end it 'ãã±ãããä½ãããã¨' do expect(Aws::S3::Resource.new(client: s3_client).bucket('mybucket')) .to be_an_instance_of Aws::S3::Bucket end it 'ãªãã¸ã§ã¯ããä½ãããã¨' do expect(s3_client.get_object(bucket: 'mybucket', key: 'myobject').body.read).to eq 'this is a pen.' end end
S3 ã® API ãªã¯ã¨ã¹ããã¹ã¿ãããæ¹ãè¯ãã£ããããã®ã ãããï¼