11using System ;
22using System . Collections . Generic ;
3+ using System . Linq . Expressions ;
34using Azure . Messaging . ServiceBus ;
45using Microsoft . AspNetCore . Builder ;
6+ using Microsoft . Extensions . Configuration ;
57using Microsoft . Extensions . DependencyInjection ;
68using Microsoft . Extensions . Options ;
79using Xunit ;
@@ -14,23 +16,31 @@ public class ServiceCollectionExtensionsTests
1416 public void AddAzureServiceBusSender_Success ( )
1517 {
1618 // Arrange
17- const string connectionString = "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YourAccessKey" ;
18- var queues = new Dictionary < string , string >
19- {
20- {
21- "InternalKey" ,
22- "AzureServiceBusQueueName"
23- }
24- } ;
25- Action < AzureServiceBusOptions > optionsAction = options =>
26- {
27- options . Queues = queues ;
28- } ;
19+ Expression < Func < IConfiguration , string > > connectionStringExpression = configuration => configuration
20+ . GetValue < string > ( "AzureServiceBusOptions:ConnectionString" ) ;
21+
22+ Action < AzureServiceBusOptions , IConfiguration > configureOptions = ( options , configuration ) => configuration
23+ . GetSection ( "AzureServiceBusOptions" )
24+ . Bind ( options ) ;
2925
3026 var services = new ServiceCollection ( ) ;
3127
28+ services
29+ . AddTransient < IConfiguration > (
30+ _ => new ConfigurationBuilder ( )
31+ . AddInMemoryCollection (
32+ new Dictionary < string , string >
33+ {
34+ [ "AzureServiceBusOptions:ConnectionString" ] = "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=YourAccessKey" ,
35+ [ "AzureServiceBusOptions:Queues:FirstInternalKey" ] = "FirstAzureServiceBusQueueName" ,
36+ [ "AzureServiceBusOptions:Queues:SecondInternalKey" ] = "SecondAzureServiceBusQueueName"
37+ }
38+ )
39+ . Build ( )
40+ ) ;
41+
3242 // Act
33- services . AddAzureServiceBusSender ( connectionString , optionsAction ) ;
43+ services . AddAzureServiceBusSender ( connectionStringExpression . Compile ( ) , configureOptions ) ;
3444
3545 // Assert
3646 var provider = services . BuildServiceProvider ( ) ;
@@ -41,7 +51,8 @@ public void AddAzureServiceBusSender_Success()
4151 var resultOptionsAction = provider . GetRequiredService < IOptions < AzureServiceBusOptions > > ( ) ;
4252 Assert . NotNull ( resultOptionsAction ) ;
4353 Assert . NotNull ( resultOptionsAction . Value ) ;
44- Assert . Equal ( queues , resultOptionsAction . Value . Queues ) ;
54+ Assert . Equal ( "FirstAzureServiceBusQueueName" , resultOptionsAction . Value . Queues [ "FirstInternalKey" ] ) ;
55+ Assert . Equal ( "SecondAzureServiceBusQueueName" , resultOptionsAction . Value . Queues [ "SecondInternalKey" ] ) ;
4556
4657 var resultAzureServiceBusSender = provider . GetRequiredService < IAzureServiceBusSender > ( ) ;
4758 Assert . NotNull ( resultAzureServiceBusSender ) ;
0 commit comments