Creating and implementing the WalksEntryPageViewModelTest class

In this section, we'll take a look at how to create the WalkEntryPageViewModelTest class that will be used for our second unit test to ensure that our ViewModel is properly initialized after the Init instance method is called to determine if our unit test passes or fails under each of the different test conditions.

Let's see how we can achieve this by performing the following steps:

  1. Right-click on the TrackMyWalks.UnitTests project and choose Add | New File... from the pop-up menu. Then, choose the Empty Class option under the General section, as you did in the section entitled Creating and implementing the WalksMainPageViewModelTest located within this chapter.
  2. Next, enter WalkEntryPageViewModelTest for the name of the class to be created and click on the New button to proceed. Then, with the WalkEntryPageViewModelTest.cs file open, enter the following code snippet:
    //
// WalkEntryPageViewModelTest.cs
// Unit Test of the WalkEntryPageViewModel
//
// Created by Steven F. Daniel on 14/08/2018
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using System.Threading.Tasks;
using Moq;
using TrackMyWalks.Services;
using TrackMyWalks.ViewModels;
using Xunit;

namespace TrackMyWalks.UnitTest
{
public class WalkEntryPageViewModelTest
{
[Fact]
public async Task CheckIfEntryTitleIsEqual()
{
var navMock = new Mock<INavigationService>().Object;
var viewModel = new WalkEntryPageViewModel(navMock);
// Arrange
viewModel.Title = "New Walk Entry";
// Act
await viewModel.Init();
// Assert
Assert.Equal("New Walk Entry", viewModel.Title);
}
[Fact]
public async Task CheckIfDifficultyIsEqual()
{
var navMock = new Mock<INavigationService>().Object;
var viewModel = new WalkEntryPageViewModel(navMock);
// Arrange
viewModel.Difficulty = "Easy";
// Act
await viewModel.Init();
// Assert
Assert.Equal("Hard", viewModel.Difficulty);
}
[Fact]
public async Task CheckIfDistanceIsNotEqual()
{
var navMock = new Mock<INavigationService>().Object;
var viewModel = new WalkEntryPageViewModel(navMock);
// Arrange
viewModel.Distance = 256;
// Act
await viewModel.Init();
// Assert
Assert.NotEqual("0", viewModel.Difficulty);
}
}
}

Now, let's start by taking a look at what we covered in the preceding code snippet:

  1. First, we included references to the System.Threading.Tasks, Moq, and Xunit namespaces, so we have access to the class method implementations that are defined within these namespaces. You'll notice that we have also included references to our TrackMyWalks.Services and TrackMyWalks.ViewModels namespaces, so that we can access our ViewModels and NavigationService class.
  2. Next, within the WalkEntryPageViewModelTest class, we defined the [Fact] attribute to indicate that our method should be run by the Xunit Test Runner component. Then, we created our CheckIfEntryTitleIsEqual instance method that will handle asynchronous calls as well as perform a test to see if the Title property contained within our WalkEntryPageViewModel is equal. We then declared a navMock variable instance of the Mock class that is contained within our Moq library in order to create a new instance of our INavigationService interface.
  3. Then, we initialized the Title property within our ViewModel to the value that we want to check and called the Init instance method. We then used the Equal method on the Assert class to determine after our ViewModel was initialized whether the Title property matches the value we want to check to determine if our test passes or fails.
  4. Next, we created our CheckIfDifficultyIsEqual instance method that will handle asynchronous calls as well as perform a test to see if the Difficulty property is equal. We declared a navMock variable instance of the Mock class that is contained within our Moq library,to create a new instance of our INavigationService interface.
  5. Then, we initialized the Title property within our ViewModel to the value that we want to check, prior to calling the Init instance method, and used the Equal method on the Assert class to determine if the Difficulty property matches the value we want to check. We do this to determine if our test passes or fails.
  6. Next, we created our CheckIfDistanceIsNotEqual instance method that will handle asynchronous calls as well as perform a test to see if the Distance property is equal. We declared a navMock variable instance of the Mock class that is contained within our Moq library to create a new instance of our INavigationService interface.
  1. Finally, we initialized the Distance property within our ViewModel to the value that we want to check, prior to calling the Init instance method, and used the NotEqual method on the Assert class to determine if the Distance property matches the value we want to check. We do this to determine if our test passes or fails.

You will notice that for each of the unit tests that we created in the preceding code snippets, they were presented with the [Fact] attribute, as well as the Arrange-Act-Assert pattern. The following table provides a brief description of what each of the Arrange-Act-Assert patterns are used for:

Pattern

Description

Arrange

The Arrange test pattern will essentially perform all of the setting up and initialization conditions for your test.

Act

The Act test pattern will ensure that your test will successfully interact with the application.

Assert

The Assert test pattern will examine the results of the actions that were initially performed within the Act step to verify the results.

 

For more information on the Arrange-Act-Assert pattern, refer to the Unit Testing Enterprise Apps documentation at https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/unit-testing.

Now that you have created your unit tests and have a reasonably good understanding of what each of the Arrange-Act-Assert patterns are, our next step is to begin running our unit tests within the Visual Studio for Mac IDE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181