O'Reilly logo

Professional Test-Driven Development with C#: Developing Real World Applications with TDD by Jeff McWherter, James Bender

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required