开发者

What did I do wrong here when trying to unit test a class that references a web service

开发者 https://www.devze.com 2023-01-02 13:09 出处:网络
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.

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号