public async Task LogsInformationToILogger() { // Arrange var memoryStorage = new MemoryStorage(); var conversationState = new ConversationState(memoryStorage); var userState = new UserState(memoryStorage); var mockRootDialog = SimpleMockFactory.CreateMockDialog <Dialog>(null, "mockRootDialog"); var mockLogger = new Mock <ILogger <DialogBot <Dialog> > >(); mockLogger.Setup(x => x.Log(It.IsAny <LogLevel>(), It.IsAny <EventId>(), It.IsAny <object>(), null, It.IsAny <Func <object, Exception, string> >())); // Run the bot var sut = new DialogBot <Dialog>(conversationState, userState, mockRootDialog.Object, mockLogger.Object); var testAdapter = new TestAdapter(); var testFlow = new TestFlow(testAdapter, sut); await testFlow.Send("Hi").StartTestAsync(); // Assert that log was changed with the expected parameters mockLogger.Verify( x => x.Log( LogLevel.Information, It.IsAny <EventId>(), It.Is <object>(o => o.ToString() == "Running dialog with Message Activity."), null, It.IsAny <Func <object, Exception, string> >()), Times.Once); }
public async Task SavesTurnStateUsingMockWithVirtualSaveChangesAsync() { // Note: this test requires that SaveChangesAsync is made virtual in order to be able to create a mock. var memoryStorage = new MemoryStorage(); var mockConversationState = new Mock <ConversationState>(memoryStorage) { CallBase = true, }; var mockUserState = new Mock <UserState>(memoryStorage) { CallBase = true, }; var mockRootDialog = SimpleMockFactory.CreateMockDialog <Dialog>(null, "mockRootDialog"); var mockLogger = new Mock <ILogger <DialogBot <Dialog> > >(); // Act var sut = new DialogBot <Dialog>(mockConversationState.Object, mockUserState.Object, mockRootDialog.Object, mockLogger.Object); var testAdapter = new TestAdapter(); var testFlow = new TestFlow(testAdapter, sut); await testFlow.Send("Hi").StartTestAsync(); // Assert that SaveChangesAsync was called mockConversationState.Verify(x => x.SaveChangesAsync(It.IsAny <TurnContext>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Once); mockUserState.Verify(x => x.SaveChangesAsync(It.IsAny <TurnContext>(), It.IsAny <bool>(), It.IsAny <CancellationToken>()), Times.Once); }
public async Task DialogFlowUseCases(TestDataObject testData) { // Arrange var bookingTestData = testData.GetObject <BookingDialogTestCase>(); var mockFlightBookingService = new Mock <IFlightBookingService>(); mockFlightBookingService .Setup(x => x.BookFlight(It.IsAny <BookingDetails>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(bookingTestData.FlightBookingServiceResult)); var mockGetBookingDetailsDialog = SimpleMockFactory.CreateMockDialog <GetBookingDetailsDialog>(bookingTestData.GetBookingDetailsDialogResult).Object; var sut = new BookingDialog(mockGetBookingDetailsDialog, mockFlightBookingService.Object); var testClient = new DialogTestClient(Channels.Test, sut, middlewares: _middlewares); // Act/Assert Output.WriteLine($"Test Case: {bookingTestData.Name}"); for (var i = 0; i < bookingTestData.UtterancesAndReplies.GetLength(0); i++) { var utterance = bookingTestData.UtterancesAndReplies[i, 0]; // Send the activity and receive the first reply or just pull the next // activity from the queue if there's nothing to send var reply = !string.IsNullOrEmpty(utterance) ? await testClient.SendActivityAsync <IMessageActivity>(utterance) : testClient.GetNextReply <IMessageActivity>(); Assert.Equal(bookingTestData.UtterancesAndReplies[i, 1], reply.Text); } Assert.Equal(bookingTestData.ExpectedDialogResult.Status, testClient.DialogTurnResult.Status); }
public async Task ReturnsWelcomeCardOnConversationUpdate() { // Arrange var mockRootDialog = SimpleMockFactory.CreateMockDialog <Dialog>(null, "mockRootDialog"); // TODO: do we need state here? var memoryStorage = new MemoryStorage(); var sut = new DialogAndWelcomeBot <Dialog>(new ConversationState(memoryStorage), new UserState(memoryStorage), mockRootDialog.Object, null); var conversationUpdateActivity = new Activity { Type = ActivityTypes.ConversationUpdate, MembersAdded = new List <ChannelAccount> { new ChannelAccount { Id = "theUser" }, }, Recipient = new ChannelAccount { Id = "theBot" }, }; var testAdapter = new TestAdapter(Channels.Test); // Act // Note: it is kind of obscure that we need to use OnTurnAsync to trigger OnMembersAdded so we get the card await testAdapter.ProcessActivityAsync(conversationUpdateActivity, sut.OnTurnAsync, CancellationToken.None); var reply = testAdapter.GetNextReply(); // Assert var m = (IMessageActivity)reply; Assert.Equal(1, m.Attachments.Count); Assert.Equal("application/vnd.microsoft.card.adaptive", m.Attachments.FirstOrDefault()?.ContentType); }
public async Task WholeEnchilada() { var mockFlightBookingService = new Mock <IFlightBookingService>(); mockFlightBookingService.Setup(x => x.BookFlight(It.IsAny <BookingDetails>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); var mockBookingDialog = SimpleMockFactory.CreateMockDialog <BookingDialog>(null, mockFlightBookingService.Object).Object; var mockLogger = new Mock <ILogger <MainDialog> >(); var sut = new MainDialog(mockLogger.Object, null, mockBookingDialog); var testFlow = BuildTestFlow(sut); await testFlow.Send("hi") .AssertReply("What can I help you with today?") .Send("hi") .AssertReply("Where would you like to travel to?") .Send("Bahamas") .AssertReply("Where are you traveling from?") .Send("New York") .AssertReply("When would you like to travel?") .Send("tomorrow at 5 PM") .AssertReply(activity => { // TODO: I had to add the Yes No for the channelId = test, the emulator displays suggested actions instead. var message = (IMessageActivity)activity; Assert.Equal( "Please confirm, I have you traveling to: Bahamas from: New York on: 2019-04-18T17 (1) Yes or (2) No", message.Text); }) .Send("Yes") .AssertReply("I have you booked to Bahamas from New York on tomorrow 5PM") .StartTestAsync(); }
public MainDialogTests(ITestOutputHelper output) : base(output) { _mockLogger = new Mock <ILogger <MainDialog> >(); var mockFlightBookingService = new Mock <IFlightBookingService>(); mockFlightBookingService .Setup(x => x.BookFlight(It.IsAny <BookingDetails>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(true)); _mockBookingDialog = SimpleMockFactory.CreateMockDialog <BookingDialog>(null, new Mock <GetBookingDetailsDialog>().Object, mockFlightBookingService.Object).Object; }
public MainDialogTests(ITestOutputHelper output) : base(output) { _mockLogger = new Mock <ILogger <MainDialog> >(); var expectedBookingDialogResult = new BookingDetails() { Destination = "Seattle", Origin = "New York", TravelDate = $"{DateTime.UtcNow.AddDays(1):yyyy-MM-dd}" }; _mockBookingDialog = SimpleMockFactory.CreateMockDialog <BookingDialog>(expectedBookingDialogResult).Object; }
public async Task ReturnsWelcomeCardOnConversationUpdate() { // Arrange var mockRootDialog = SimpleMockFactory.CreateMockDialog <Dialog>(null, "mockRootDialog"); var memoryStorage = new MemoryStorage(); var sut = new DialogAndWelcomeBot <Dialog>(new ConversationState(memoryStorage), new UserState(memoryStorage), mockRootDialog.Object, null); // Create conversationUpdate activity var conversationUpdateActivity = new Activity { Type = ActivityTypes.ConversationUpdate, MembersAdded = new List <ChannelAccount> { new ChannelAccount { Id = "theUser" }, }, Recipient = new ChannelAccount { Id = "theBot" }, }; var testAdapter = new TestAdapter(Channels.Test); // Act // Send the conversation update activity to the bot. await testAdapter.ProcessActivityAsync(conversationUpdateActivity, sut.OnTurnAsync, CancellationToken.None); // Assert we got the welcome card var reply = (IMessageActivity)testAdapter.GetNextReply(); Assert.Equal(1, reply.Attachments.Count); Assert.Equal("application/vnd.microsoft.card.adaptive", reply.Attachments.FirstOrDefault()?.ContentType); // Assert that we started the main dialog. reply = (IMessageActivity)testAdapter.GetNextReply(); Assert.Equal("Dialog mock invoked", reply.Text); }