93 CHAPTER 12 | Unit testing var mockViewModel = new MockViewModel(); mockViewModel.Forename.Value = "John"; mockViewModel.Surname.Value = "Smith"; var isValid = mockViewModel.Validate(); Assert.True(isValid); } This unit test checks that validation succeeds when the two ValidatableObject<T> properties in the MockViewModel instance both have data. As well as checking that validation succeeds, validation unit tests should also check the values of the Value, IsValid, and Errors property of each ValidatableObject<T> instance, to verify that the class performs as expected. The following code example demonstrates a unit test that does this: [Fact] public void CheckValidationFailsWhenOnlyForenameHasDataTest() { var mockViewModel = new MockViewModel(); mockViewModel.Forename.Value = "John"; bool isValid = mockViewModel.Validate(); Assert.False(isValid); Assert.NotNull(mockViewModel.Forename.Value); Assert.Null(mockViewModel.Surname.Value); Assert.True(mockViewModel.Forename.IsValid); Assert.False(mockViewModel.Surname.IsValid); Assert.Empty(mockViewModel.Forename.Errors); Assert.NotEmpty(mockViewModel.Surname.Errors); } This unit test checks that validation fails when the Surname property of the MockViewModel doesn’t have any data, and the Value, IsValid, and Errors property of each ValidatableObject<T> instance are correctly set. Summary A unit test takes a small unit of the app, typically a method, isolates it from the remainder of the code, and verifies that it behaves as expected. Its goal is to check that each unit of functionality performs as expected, so errors don’t propagate throughout the app. The behavior of an object under test can be isolated by replacing dependent objects with mock objects that simulate the behavior of the dependent objects. This enables unit tests to be executed without requiring unwieldy resources such as runtime platform features, web services, or databases Testing models and view models from MVVM applications is identical to testing any other classes, and the same tools and techniques can be used.