I am developing one Windows phone 7 application. It has another project for unit testing. Now in the main application I have one method that executes some code to navigate to another page. While unit testing I don’t want that method to execute and just return in the first line. Right now how I am doing it is defining a symbol while unit testing.
#define EnableUnitTest
public static void Navigate(string page)
{
#if EnableUnitTest
return;
#endif
.....
.....
}
Is there a better way of doing it.
Sorry to say it but that seems like a very bad design. In a unit test you are testing some code which shouldn’t be conditioned that way. A unit test should consist of three steps: Arrange, Act, Assert where you prepare the input, call the actual method and assert the results. It’s in the arrange step that you should prepare the method under test.
Also I notice that this method is static. Static methods are difficult to unit test. It would be better to abstract the logic of this class into an interface or abstract base class which could be easily mocked/stubbed in a unit test to weaken the coupling between the different parts of your code.