SPEC: improve the spec using stubbed S3 client.

SPEC: improve the spec using stubbed S3 client.

4d1204b5e8f934e2cb333d0be15b555c2a457a89

diff --git a/spec/components/s3_helper_spec.rb b/spec/components/s3_helper_spec.rb
index d4176ff..789c61a 100644
--- a/spec/components/s3_helper_spec.rb
+++ b/spec/components/s3_helper_spec.rb
@@ -4,6 +4,8 @@ require "s3_helper"
 require "rails_helper"
 
 describe "S3Helper" do
+  let(:client) { Aws::S3::Client.new(stub_responses: true) }
+
   before(:each) do
     SiteSetting.enable_s3_uploads = true
     SiteSetting.s3_access_key_id = "abc"
@@ -70,23 +72,19 @@ describe "S3Helper" do
         'some/bucket' => 'bucket/testing',
         'some' => 'testing'
       }.each do |bucket_name, prefix|
-        s3_helper = S3Helper.new(bucket_name)
-        bucket = stub('s3_bucket')
-        s3_helper.expects(:s3_bucket).returns(bucket)
-        bucket.expects(:objects).with(prefix: prefix)
+        s3_helper = S3Helper.new(bucket_name, "", client: client)
+        Aws::S3::Bucket.any_instance.expects(:objects).with(prefix: prefix)
         s3_helper.list('testing')
       end
     end
   end
 
   it "should prefix bucket folder path only if not exists" do
-    s3_helper = S3Helper.new('bucket/folder_path')
-    bucket = stub('s3_bucket')
+    s3_helper = S3Helper.new('bucket/folder_path', "", client: client)
 
-    s3_helper.expects(:s3_bucket).returns(bucket).twice
-    bucket.expects(:object).with("folder_path/original/1X/def.xyz").twice
+    object1 = s3_helper.object("original/1X/def.xyz")
+    object2 = s3_helper.object("folder_path/original/1X/def.xyz")
 
-    s3_helper.object("original/1X/def.xyz")
-    s3_helper.object("folder_path/original/1X/def.xyz")
+    expect(object1.key).to eq(object2.key)
   end
 end

GitHub sha: 45aebd00

1 Like