WPF and MVVM: Test Driven Development of ViewModels

Testing your ViewModels while they're in development can help you create better apps. In this course, you'll learn how to use test-driven development (TDD) to build the ViewModels of your MVVM application.
Course info
Rating
(109)
Level
Intermediate
Updated
Apr 12, 2016
Duration
5h 22m
Table of contents
Course Overview
Introduction
Getting Started with Unit Testing
Writing Testable ViewModels
Mocking Dependencies with Moq
Testing and Implementing the Loading of Friends
Closing Tabs and Saving a Friend
Adding a New Friend
Deleting an Existing Friend
Interacting with Dialogs in MVVM
Description
Course info
Rating
(109)
Level
Intermediate
Updated
Apr 12, 2016
Duration
5h 22m
Description

When applying the MVVM pattern, the UI-logic is placed in ViewModels. A key advantage of such a ViewModel is that it is highly testable. In this course, WPF and MVVM: Test Driven Development of ViewModels, you'll learn the specifics of unit testing within MVVM. You'll discover how to create and run unit tests in Visual Studio with the popular testing framework xUnit, and how to write testable ViewModels by moving out typical dependencies like data access logic, event aggregator, modal dialogs and more. Next, you'll be taught how to mock/fake these dependencies in your unit tests by using the mocking library, Moq, as well as inject the dependencies into your ViewModels with a dependency injection framework called Autofac while running your application. Finally, you'll build and unit-test the ViewModels for an MVVM app called FriendStorage from 'empty class' to final by using test-driven development. By the end of this course, you'll understand how to develop your ViewModels through testing in order to create better apps than ever.

About the author
About the author

Thomas is a well-known author, software developer, and speaker from Germany. He specializes in .NET, C#, TypeScript, XAML, and Azure.

More from the author
.NET Standard: Getting Started
Intermediate
2h 44m
May 14, 2018
More courses by Thomas Claudius Huber
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone! My name is Thomas Claudius Huber, and welcome to my course, WPF and MVVM: Test Driven Development of ViewModels. I am a Microsoft MVP for Windows development, and I have a passion for user interfaces. When I build a WPF application, I usually apply the MVVM pattern. The big advantage of the MVVM pattern is that you can test the complete code of your ViewModel with unit tests. This course is an introduction to test-driven development of ViewModels. Test-driven development means that you write your unit tests before you implement the actual logic. Some of the major topics that we will cover include creating unit tests in Visual Studio with xUnit, writing testable ViewModels by abstracting away dependencies, mocking dependencies with the Moq library, testing the ViewModels of the FriendStorage application, and also testing dialog interactions in MVVM. By the end of the course, you will know how to develop your ViewModels in a test-driven way to improve the quality and maintainability of your MVVM applications. For this course, no prior experience with unit testing and test-drive development is required. I hope you will join me on this journey to learn how to develop your ViewModels in a test-driven way with the course, WPF and MVVM: Test Driven Development of ViewModels at Pluralsight.

Getting Started with Unit Testing
Welcome to the module, Getting Started with Unit Testing. My name is Thomas Claudius Huber, and in this module, I will show you the basics about unit testing and test-driven development. Let's take a quick look at the module outline. We start this module by looking at the unit testing basics. I will show you what a unit test is, and I will also show you that a unit test is fast, independent, repeatable, self-validating, and timely. When we look at these unit testing basics, you will also learn about popular unit testing frameworks available for. NET. After you have learned about the unit test basics, we will write and run unit tests in Visual Studio with MSTest and xUnit. MSTest is a unit testing framework that comes with Visual Studio. xUnit is another unit testing framework that is quite popular and open source. After you have learned how to write and run unit tests with MSTest and xUnit, we will take a look at test-driven development. You will learn about the approach around red/green/refactor, and you will also learn about spikes in test-driven development. With the gained knowledge about unit testing and test-driven development, we will start unit testing in the FriendStorage application. We try to test the NavigationViewModel of FriendStorage and how it loads its navigation items. So let's start now with the unit testing basics.

Writing Testable ViewModels
Welcome to the module, Writing Testable ViewModels. My name is Thomas Claudius Huber, and in this module, I will show you how you can abstract away the dependencies from the ViewModels to make them testable. Before we start, let's take a quick look at the module outline. The first thing I want to show you in this module are the typical dependencies of a ViewModel like, for example, data access somewhere the ViewModel needs to get its data from. After you know some of these dependencies, you will learn how you can abstract away the dependencies with interfaces. This allows you to mock the dependencies in your unit tests. Abstracting away all the dependencies from the ViewModel leads to many constructor parameters. To make sure that you don't have to touch your production code every time you add a constructor parameter to your ViewModel, you will also learn how to use the dependency injection framework in your production code. So let's start and let's look at the typical dependencies of a ViewModel.

Mocking Dependencies with Moq
Welcome to the module, Mocking Dependencies with Moq. My name is Thomas Claudius Huber, and in this module, I will show you how you can use the Moq library to mock the dependencies in your unit tests without implementing classes for these dependencies. Before we do this, let's take a quick look at the module outline. We will start this module by looking at the basics of the Moq library. Then we will use that Moq library in the unit tests that we have written already for the FriendStorage application. We will replace the classes like, for example, the NavigationDataProviderMock class that we use in the NavigationViewModel tests. So let's start now with the Moq basics.

Testing and Implementing the Loading of Friends
Welcome to the module, Testing and Implementing the Loading of Friends. My name is Thomas Claudius Huber, and in this module we will extend the FriendStorage application so that a friend is displayed in a tab when he has been clicked in the NavigationView. Before we start with an overview how we want to load friends in FriendStorage, let's take quick look at the module outline so that I can show you what you will learn in this module. In this module, we will add an event aggregator to the FriendStorage application, and we will define an OpenFriendEditViewEvent. You will learn how to publish the event and how to subscribe to it in your ViewModels. And, especially, you will learn how to test it. Besides the event aggregator and the event, we will extend the ViewModels in FriendStorage with additional logic. For example, the FriendEditViewModel needs to load a friend by Id. So it needs a data provider that it can use to get that friend. And while extending the ViewModels in FriendStorage, we will write for sure a bunch of unit tests. So let's start now with an overview of the loading of friends in the FriendStorage application.

Closing Tabs and Saving a Friend
Welcome to the module, Closing Tabs and Saving a Friend. My name is Thomas Claudius Huber, and in this module, we will extend the FriendStorage application so that the user can close tabs and that he can save the friends that he has modified. Let's take a quick look at the module outline. We start this module by testing and implementing the logic to close a tab in the FriendStorage application. Then we will enable the Save button if the user has made changes to the current friend. We will also implement the actual save logic so that when the user clicks the button, the friend is saved by using the DataProvider. After a friend has been saved, we need to refresh the navigation. If the user has changed the friend's name, we also need to display the new name in the navigation after the friend has been saved. To refresh the navigation, we will use an event that we publish with the EventAggregator. Then the NavigationViewModel can subscribe to that event to update the saved friend's name. As we do all of this in a test-driven way, we have to write a lot of unit tests in this module. So we shouldn't lose more time on this module outline. Let's start with closing a tab in the FriendStorage application.

Adding a New Friend
Welcome to the module, Adding a New Friend. My name is Thomas Claudius Huber, and in this module, we will extend the FriendStorage application so that the user can add new friends. Before we start, let's take a look at the module outline. The first thing that we will do in this module is to introduce the AddCommand in the MainViewModel. We will test and implement that command. And in the FriendEditViewModel, we have to create a new Friend object. When that new Friend object has been saved, we need to refresh the navigation because the navigation does not contain that new Friend yet. So let's start with the AddCommand in the MainViewModel.

Deleting an Existing Friend
Welcome to the module, Deleting an Existing Friend. My name is Thomas Claudius Huber, and in this module we will extend the FriendStorage application so that the user can delete existing friends. Before we start, let's take a quick look at the module outline. The first thing that we do in this module is we will enable the Delete button for existing friends. When the user creates a new friend, the Delete button should be disabled as long as that new friend hasn't been saved. After we have done this, we will focus on the implementation of the delete logic. The delete logic includes deleting the friend by using the DataProvider. It includes refreshing the navigation after the delete. And it also includes closing the tab after the friend has been deleted. So, as usual, we have some work to do, and we need to write a lot of unit tests. Now let's start with enabling the Delete button only for existing friends in the FriendEditView.

Interacting with Dialogs in MVVM
Welcome to the module, Interacting With Dialogs in MVVM. My name is Thomas Claudius Huber, and in this module I will show you how you can display a dialog from your ViewModel and how you can test it. Before we start, let's take a quick look at the module outline. The first thing that I want to show you in this module is the problem with dialogs in the ViewModel. You will see that you can't use a dialog like the MessageBox directly in your ViewModel because then it's not testable. Then we will learn how to make the ViewModel with a dialog testable, and we will display the dialog in the FriendStorage application when deleting a friend. We will ask the user if he really wants to delete the friend when he clicks the Delete button. Then he can choose Yes or No. Cool! So let's start with the problems with dialogs in the ViewModel.