how to suppress message box from popping up during unit testing VB.net
Was wondering how you do this?
As it's very annoying to click "o开发者_开发技巧k" everytime a message box pops up.
Ideally, the message box is set up as a dependency so that it could be mocked/stubbed/faked during unit testing, something like this:
public interface IMessageBox
{
Show(...);
}
Of course, you would need to implement the interface in your MessageBox wrapper class. More about DI: http://en.wikipedia.org/wiki/Dependency_injection
If it's not possible to re-engineer your application this way now, you can try this approach.
Ideally, your tests would be testing your business logic. I typically view the MessageBox as a UI component rather than UI. This requires modifying the program flow so that errors/exceptions/validation happens independantly of the user notifications. IE relying on IDataErrorInfo instead of exception on property set.
In cases where you can't avoid it and don't want to go the route of wrapping the messagebox and using mocks with DI, I've used a technique of setting a Public Shared Property RunSilent As Boolean = False
singleton variable in my applications to indicate that the applciation is supposed to run silent and then check that property before calling to the messagebox to only show the box if we aren't running silent. And then in my unit test setup, set the silent flag to true. Not ideal, but effective.
精彩评论