Unit tests and Messagebox

A

Andrew Ducker

I'm writing some unit tests, to make sure that my windows forms code
works properly after refactoring, and I've bumped into a problem -
Messagebox.show.

There are a couple of places where my code pops up a messagebox to
inform the user of what's going on, and if this happens during a unit
test then it requires the user to click on the button before it can
finish the test. Which isn't much use for, say, unattended automatic
testing.

Is there any way to intercept a Messagebox call and fake it being
clicked? Or is there some other way that I should approach this?

Cheers,

Andy D
 
M

Michael Nemtsev

Hello Andrew,

Have u tried to find that window and programatically push the button?

AD> I'm writing some unit tests, to make sure that my windows forms code
AD> works properly after refactoring, and I've bumped into a problem -
AD> Messagebox.show.
AD>
AD> There are a couple of places where my code pops up a messagebox to
AD> inform the user of what's going on, and if this happens during a
AD> unit test then it requires the user to click on the button before it
AD> can finish the test. Which isn't much use for, say, unattended
AD> automatic testing.
AD>
AD> Is there any way to intercept a Messagebox call and fake it being
AD> clicked? Or is there some other way that I should approach this?
AD>
AD> Cheers,
AD>
AD> Andy D
AD>
---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
M

Michael C

Cor Ligthert said:
Andrew,

I would just make my own new form as a messagebox.

Change the app to suit the test? Shouldn't it be the other way around?

Michael
 
J

Jeff Louie

How about a debug/unit test log flag say isLogMessageBox that writes to
a log
in debug/unit test mode and pops up a message box in release mode. Me
thinks
that if you define the bool flag in the file as a const (constants are
not
versionable, must be recompiled to reflect changes), the compiler will
optimize
the code and remove the if/then logic in release mode. I have not tested
this. To
avoid mistakes, you might want to wrap the conditional call to
MessageBox.Show() into a method DebugMessageBox.Show() and replace the
appropriate calls to MessageBox.Show() with a call to the conditional
DebugTestMessageBox.Show(). In release mode, set the flag
isLogMessageBox to
false and recompile.

Regards,
Jeff
 
A

Andrew Ducker

Check out NUnitForms.

Cheers for that - the ExpectedModal isn't quite perfect, but it does
near enough what I want that I'm no longer stuck.

Cheers!

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top