I'm using Microsofts Bot Framework and I'm trying to unit test (in isolation) a Dialog that I have:
public class MyDialog : IDialog
{
public async Task StartAsync(IDialogContext context)
{
PromptDialog.Confirm(context, MessageReceived, "Are you sure?", "Sorry what was that?");
}
private async Task MessageReceived(IDialogContext context, IAwaitable<bool> result)
{
bool isSure = await result;
string response = isSure ? "Awesome" : "Sorry";
IMessageActivity messageActivity = context.MakeMessage();
messageActivity.Text = response;
await context.PostAsync(messageActivity);
context.Done<object>(null);
}
}
I want to prove that if the IAwaitable result comes in as true, it replies with "Awesome" and if its false its "Sorry".
PromptDialog is a class with a static method Confirm
I have unit tested dialogs before successfully using moq to mock the IMessageActivity and IDialogContext that is passed into the dialog. This feels more complicated because I want to mock a state of the dialog.
So Far:
[TestFixture]
public class Tests
{
private Mock<IDialogContext> _dialogContext;
private Mock<IMessageActivity> _messageActivity;
private MyDialog _myDialog;
[SetUp]
public void Setup()
{
_dialogContext = new Mock<IDialogContext>();
_messageActivity = new Mock<IMessageActivity>();
_messageActivity.SetupAllProperties();
_dialogContext.SetupSequence(x => x.MakeMessage())
.Returns(_messageActivity.Object);
_myDialog = new MyDialog();
}
[Test]
public void GivenTrue_WhenIConfirmPrompt_ThenAwesome()
{
_myDialog
.StartAsync(_dialogContext.Object)
.Wait(CancellationToken.None);
Assert.That(_messageActivity.Object.Text, Is.EqualTo("Awesome"));
}
[Test]
public void GivenTrue_WhenIRejectPrompt_ThenSorry()
{
_myDialog
.StartAsync(_dialogContext.Object)
.Wait(CancellationToken.None);
Assert.That(_messageActivity.Object.Text, Is.EqualTo("Sorry"));
}
}
Does anyone have any suggestions or ideas how to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…