views:

41

answers:

2

So I had a class that referenced a class that referenced another class that called a web service.

So I learn how to create an interface using partial classes.

I inject the web service through the constructor.

Then my unit test fails because I am newing up the actual web service in the second level of the class. So I end up modifying all three classes to pass the web service down through the constructor... was not happy :-( gave up....

what should I be doing in this case?

+4  A: 

You should be using interfaces instead of partial classes. You constructor than expects an interface instead of the concrete implementation. In the tests instead of creating the real class you create a "mock" using one of the mocking frameworks, e.g. I am using Rhino Mocks, you can find loads of examples on their documentation page: http://ayende.com/wiki/Rhino+Mocks+Documentation.ashx

Grzenio
Bang on. You need a mocking framework in order to do this properly.
Andrew Anderson
+1  A: 

The layer which interacts with web service should be designed to have an interface. This should be injected (passed thru ctor/property/method..some other ways too exist) to other layers. The production code will use actually web service implementation. The test code will use a mock object.If you post some code, you will get better answers.

P.K