Skip to content

Commit fb87b36

Browse files
isdanielppittle
authored andcommitted
add unit test for InjectS3ClientByDIObjects
1 parent 3c4f021 commit fb87b36

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

extensions/test/NETCore.SetupTests/DependencyInjectionTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,66 @@ public void TryAddServiceDontOverrideWhenAlreadySetup()
7474
Assert.True(s3.GetType() == mockS3.GetType());
7575
}
7676

77+
[Fact]
78+
public void CreateAWSOptionsByDIObjects()
79+
{
80+
RegionEndpoint expectRegion = RegionEndpoint.APSouth1;
81+
string expectProfile = "MockProfile";
82+
83+
ServiceCollection services = new ServiceCollection();
84+
services.AddSingleton(new AWSSetting() {
85+
Region = expectRegion,
86+
Profile = expectProfile
87+
});
88+
89+
services.AddDefaultAWSOptions(sp=> {
90+
var setting = sp.GetRequiredService<AWSSetting>();
91+
return new AWSOptions()
92+
{
93+
Region = setting.Region,
94+
Profile = setting.Profile
95+
};
96+
});
97+
98+
var serviceProvider = services.BuildServiceProvider();
99+
var awsOptions = serviceProvider.GetRequiredService<AWSOptions>();
100+
101+
Assert.NotNull(awsOptions);
102+
Assert.Equal(expectRegion, awsOptions.Region);
103+
Assert.Equal(expectProfile, awsOptions.Profile);
104+
}
105+
106+
[Fact]
107+
public void InjectS3ClientByDIConfigs()
108+
{
109+
RegionEndpoint expectRegion = RegionEndpoint.APSouth1;
110+
string expectProfile = "MockProfile";
111+
112+
ServiceCollection services = new ServiceCollection();
113+
services.AddSingleton(new AWSSetting()
114+
{
115+
Region = expectRegion,
116+
Profile = expectProfile
117+
});
118+
119+
services.AddDefaultAWSOptions(sp => {
120+
var setting = sp.GetRequiredService<AWSSetting>();
121+
return new AWSOptions()
122+
{
123+
Region = setting.Region,
124+
Profile = setting.Profile
125+
};
126+
});
127+
128+
services.AddAWSService<IAmazonS3>();
129+
130+
var serviceProvider = services.BuildServiceProvider();
131+
132+
var controller = ActivatorUtilities.CreateInstance<TestController>(serviceProvider);
133+
Assert.NotNull(controller.S3Client);
134+
Assert.Equal(expectRegion, controller.S3Client.Config.RegionEndpoint);
135+
}
136+
77137
public class TestController
78138
{
79139
public IAmazonS3 S3Client { get; private set; }
@@ -82,5 +142,11 @@ public TestController(IAmazonS3 s3Client)
82142
S3Client = s3Client;
83143
}
84144
}
145+
146+
internal class AWSSetting {
147+
public RegionEndpoint Region { get; set; }
148+
149+
public string Profile { get; set; }
150+
}
85151
}
86152
}

0 commit comments

Comments
 (0)