views:

94

answers:

3

I am looking to skip a certain statement in my unit tests eg:

if (MessageBox.Show("Are you sure you want to remove " + contact.CompanyName + " from the contacts?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.Yes)

is there an attribute i can place above the statement to avoid the unit test executing it?

+3  A: 

you should try to avoid directly calling GUI components in the classes you are unit testing. One way around this would be to create an interface, say IMessageBoxDisplayService, which can be stubbed out for the test

public MyClass(IMessageBoxDisplayService messageBoxDisplayService)
{ 
    ...
    if (messageBoxDisplayService.Show(message)) ...
Mark Heath
Ok sounds like the best way forward, but is there an attribute that will skip over a statement in the interface- regardless of the statement?
Eli Perpinyal
You can use the Conditional attribute to skip over a method.
Daniel Rose
+2  A: 

You can also try using Moles to stub out the call to MessageBox.Show().

Daniel Rose
A: 

You're probably thinking of the [Conditional()] attribute:

http://msdn.microsoft.com/en-us/library/aa664622%28VS.71%29.aspx

I think you're better off with an Mark's interface solution though.

Ray