Forum Discussion
Lakshmi_145
Dec 21, 2022Iron Contributor
Alternative method for using Middleware in CloudAdapter
In BotFrameworkAdapter ,we have method called 'Use(IMiddleware middleware)' to add middleware to adapter pipeline. We were able to use this method to unit test the middleware functions. Now we ha...
Lakshmi_145
Dec 29, 2022Iron Contributor
I wanted to add the middleware to cloud adapter for unit testing.
In ConnectNamedPipeAsync(string pipeName, IBot bot, string audience = null) , its passing bot as parameter. Instead we need to add middleware to cloud adapter for testing purpose
ChetanSharma-msft
Microsoft
Jan 20, 2023Hello Lakshmi_145 - Could you please share your code snippets that you have used for 'Use(IMiddleware middleware)' in BotFrameworkAdapter and how are you trying it for CloudAdapter for your unit tests?.
So that we can check on it from our end?
- Lakshmi_145Jan 20, 2023Iron Contributor
Use method to add the middleware in Bot framework adapter:
var adapter = new BotFrameworkAdapterTest(mockCredentialProvider.Object) .Use(newMiddleware(userState, Repository.Object, logger.Object)); // Act await adapter.ProcessActivityAsync( mockClaims.Object, new Activity() { Conversation = conversationReference.Conversation, From = conversationReference.User, Recipient = conversationReference.Bot, ChannelId = conversationReference.ChannelId, ServiceUrl = conversationReference.ServiceUrl }, async (context, token) => { activity = context.Activity; var userStateAccessors = userState.CreateProperty<UserDetailState>(nameof(UserDetailState)); userProfile = await userStateAccessors.GetAsync(context, () => new UserDetailState()); }, CancellationToken.None);
BotFrameworkAdapterTest Class:
public class BotFrameworkAdapterTest : BotFrameworkAdapter { public BotFrameworkAdapterTest(ICredentialProvider credentialProvider) : base(credentialProvider) { } public override Task<IList<ChannelAccount>> GetConversationMembersAsync(ITurnContext turnContext, CancellationToken cancellationToken) { ConversationReference conversationReference = JsonConvert.DeserializeObject<ConversationReference>(File.ReadAllText(AppContext.BaseDirectory + "//TestData//ConversationReferenceTestData_Teams.json")); IList<ChannelAccount> result = new List<ChannelAccount>() { new ChannelAccount() { AadObjectId = conversationReference.User.AadObjectId, Id = conversationReference.User.Id, Name = conversationReference.User.Name, Properties = conversationReference.User.Properties, Role = conversationReference.User.Role } }; return Task.FromResult(result); } }
Same I need to test using Cloud adapter by calling that middleware directly using adapter