How to avoid a ShowDialog() with a Mock?

1,401 views
Skip to first unread message

Philippe Bourque

unread,
Aug 26, 2009, 4:27:59 PM8/26/09
to Moq Discussions
Hello, I'm really new to Mock, .Net and C#.
I'm just trying to switch from a computer engineering background to
get new software engineering skills.

So, I'm trying to make a unit test for a method that contains a
ShowDialog() call.
Any idea how to work around the dialog with a mock?

Thank you very much,

Philippe Bourque

Chris Missal

unread,
Aug 26, 2009, 11:31:01 PM8/26/09
to moq...@googlegroups.com
Can you show your method's code? I believe I have a suggestion, but I don't want to post it if it doesn't make sense for your situation.

Emanuele DelBono

unread,
Aug 27, 2009, 3:23:48 AM8/27/09
to moq...@googlegroups.com
The ShowDialog method is not virtual so it cannot be mocked.
In general you should inject in you tested class an interface (IForm
for example) that expose the showdialog method so you can mock it.

ema
http://blog.codiceplastico.com

Chris Missal

unread,
Aug 27, 2009, 3:59:33 AM8/27/09
to moq...@googlegroups.com
To expand on Emanuele's point, here's some psuedocode (in C#):

=============================================================
Instead of something like this:
=============================================================

public class YourClassUnderTest
{
public YourClassUnderTest()
{
}

public void ShowMessage(User user)
{
if(user.IsLoggedIn)
{
ShowDialog(firstThing);
}
else
{
ShowDialog(somethingElse);
}
}
}

=============================================================
Try something like this:
=============================================================

public interface IDialogProvider
{
void ShowDialog(object something);
}

public class YourClassUnderTest
{
private IDialogProvider dialogProvider;
public YourClassUnderTest(IDialogProvider dialogProvider)
{
this.dialogProvider = dialogProvider;
}
public void ShowMessage(User user)
{
if(user.IsLoggedIn)
{
dialogProvider.ShowDialog(firstThing);
}
else
{
dialogProvider.ShowDialog(somethingElse);
}
}
}

=============================================================

Then you can provide a fake (or mock) to YourClassUnderTest and 
the implementation of ShowDialog is up to your test. It could 
just do nothing, but you check that it was called with certain
parameters.

I don't do a lot of winforms programming, but hopefully this will get his point across. If this situation isn't what you're running into, let us know. Emanuele, if this isn't what you meant, sorry, this is what I understood you were speaking of.

To get a bit more out of what I'm attempting to do with this example code, look up articles on the "Dependency Inversion Principle" and "Inversion of Control". These two programming techniques will make testing and mocking much, much easier.

Philippe Bourque

unread,
Aug 27, 2009, 8:31:17 AM8/27/09
to Moq Discussions
Thank you very much, that is exactly what I was looking for.

Now, I'll just have to convince my architect that mocking is great
enough to change the code :)

Philippe Bourque
> <codiceplast...@gmail.com>wrote:
>
>
>
>
>
>
>
> > The ShowDialog method is not virtual so it cannot be mocked.
> > In general you should inject in you tested class an interface (IForm
> > for example) that expose the showdialog method so you can mock it.
>
> > ema
> >http://blog.codiceplastico.com
>
> > On Wed, Aug 26, 2009 at 10:27 PM, Philippe Bourque<philippe...@gmail.com>
Reply all
Reply to author
Forward
0 new messages