TESTING WCF SERVICES

Unlike domain services or entities, you do not have direct control over the instantiation of your WCF services. This has led to the belief that WCF services cannot take advantage of dependency injection and therefore cannot be unit-tested. This is a fallacy. Most of the popular dependency injection frameworks available today provide some facility for injecting dependencies into WCF services. All you need are some changes to how the service implementation is constructed, and a couple of minor tweaks to how the WCF projects are configured.

Refactoring for Testability

Like domain services and entities, in an ideal world you write the tests for your WCF services before you write the code. Test first is a major tenet of TDD (remember, the second D stands for driven). This chapter starts with an existing WCF service and refactors it to make it testable. There are a couple reasons for this demonstration. You are far more likely to encounter codebases where the domain-specific code, and even the ASP.NET MVC or WPF interfaces, have been designed for testability — specifically, the ability to accept dependencies. WCF services are in the unique position of being relatively easy to refactor to accept dependencies. The service is an interface. As such, it is at the edge of the application, which means that it should have no references that would break from the creation of a new constructor to accept dependencies. Additionally, because access to services is done via proxy, ...

Get Professional Test-Driven Development with C#: Developing Real World Applications with TDD now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.