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

THE PROBLEM WITH TESTING THE USER INTERFACE

Even though XAML enforces a clean separation of the user interface and your logic, using the code-behind file actually tightly couples the two. This coupling means that you can't instantiate the code-behind without the user interface. In a code-behind-based application, all user code that handles user interactions, validation, and even calls to the database or other services can end up in the code-behind. In an automated test, you don't really want to be forced to create the UI just to test the logic behind it. It's possible to create the UI from a unit test, but this makes testing more difficult and error-prone. It becomes even more difficult when the code-behind interacts with other services.

To write automated tests for WPF and Silverlight applications, you need to fully separate the logic from the user interface in such a way that you can instantiate one without the other. There are many ways to do this, but one pattern has recently emerged and become the de facto standard for WPF development and is becoming more popular in Silverlight development, as discussed next.

The MVVM Pattern

Although it's not a silver bullet, the Model-View-ViewModel (MVVM, sometimes pronounced “moovem”) pattern facilitates testing by putting screen-level code into a class that's completely separated from XAML. Not only does MVVM help with testability, but it also helps with reusability. If you work with a user experience (UX) designer, you can focus solely ...

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