I am writing NUnit testing code for my WPF C# application. Here some of my methods having MessageBox.Show("");, but we dont know how to handle this in code.
Please help me by providing a solution.
Thanks,
I am writing NUnit testing code for my WPF C# application. Here some of my methods having MessageBox.Show("");, but we dont know how to handle this in code.
Please help me by providing a solution.
Thanks,
You could create a kind of MessageBoxService that you could Mock in your test. An example code is:
public class ClassUnderTest
{
public IMessageBoxService MessageBoxService { get; set; }
public void SomeMethod()
{
//Some logic
MessageBoxService.Show("message");
//Some more logic
}
}
interface IMessageBoxService
{
void Show(string message);
}
public class MessageBoxService : IMessageBoxService
{
public void Show(string message)
{
MessageBox.Show("");
}
}
Then in your test you could choose to mock the public property or create the constructor to pass the mocked instance. For example if you use Moq the test could look like this:
[Test]
public void ClassUnderTest_SomeMethod_ExpectsSomtething()
{
ClassUnderTest testClass = new ClassUnderTest();
testClass.MessageBoxService = new Mock<IMessageBoxService>().Object;
//More setup
//Action
//Assertion
}