Introduction to Testing

Duration

20 minutes

Lab Goals

The primary goal of this exercise is to create and run unit tests.

  1. Add a new Unit Test project to the solution
  2. Write tests for the Phoneword app

Steps

Create the Unit Test Project

  1. Open the Start solution in the Exercise 2 folder.
  2. Add a new Visual C# > Class Library (.NET Framework) project and name it Phoneword.UnitTests.Core.
  3. Install the latest NUnit and NUnit3TestAdapter NuGet packages into this new project.
  4. Add a new NUnit Library Project and name it Phoneword.UnitTests.Core.
  5. Double click on the unit test project to bring up the project options. Select the Build node, then change the Target Framework to Mono / .NET 4.5, then click OK.

Reference Phoneword Project

  1. Right click on the References for the test project and add a project reference to the Phoneword.Core project.
  2. In the new class library project, rename the default class file to TestTranslation.cs.
  3. Inside the TestTranslation.cs file, replace the contents with this code to start the beginning of our test fixture.
    using NUnit.Framework;
    using Phoneword.Core;
    
    namespace Phoneword.UnitTests.Core
    {
        [TestFixture]
        public class TestTranslation
        {
            [Test]
            public void TestXamarinPhoneNumber()
            {
            }
        }
    }
    
  4. In the test project, rename the boilerplate test file (likely named Test.cs) to TestTranslation.cs
  5. Add using Phoneword.Core; to the file so we can get to the PhonewordTranslator class.

Create a Unit Test

  1. Rename the existing TestCase method to TestXamarinPhoneNumber
  2. Add code to the TestXamarinPhoneNumber method to perform the following test:
    • Invoke the PhonewordTranslator.ToNumber method with the phone number "1-855-XAMARIN"
    • Verify that the returning number is "1-855-9262746" using the Assert class.
  3. Show Code

Create a testable mock IDialer

  1. Create a mock implementation of IDialer which captures the value of the passed number and tracks whether the Dial method was called. You can use the following implementation:
  2. public class MockDialer : IDialer
    {
       public string LastDialedNumber = null;
       public bool CalledDialer = false;
    
       public bool Dial(string number)
       {
          CalledDialer = true;
          LastDialedNumber = number;
          return true;
       }
    }
    

Create a ViewModel Test class

Let's write a unit test for the dialing code. We supply our mock IDialer to the MainViewModel.

  1. Create a new unit test by adding a new C# class named TestPhoneTranslateViewModel the test project.
  2. Copy in the TestFixture code used in the previous test.
  3. Add using Phoneword.Core; to the namespace references.
  4. Show Code

Setup the Test Class

  1. We need to setup the test fixture by creating a new MainViewModel which will invoke the dialer and a new instance of the PhoneTranslateViewModel which translates the number. Capture both of these in class-level fields.
    • Use a [SetUp] attribute on a method - this will get called prior to each of your tests.
    • Create an instance of the MockDialer and save it off in a field.
    • Name the MainViewModel field appViewModel and the PhoneTranslateViewModel field translateViewModel.
    • Pass in the MockDialer instance into your MainViewModel constructor.
    • Pass the MainViewModel into the PhoneTranslateViewModel constructor.
  2. Show Code

Test the Dial Button

  1. Create a test method to validate that the dial button is not enabled until a phone number is entered. To do this, we will create a new method which sets the phone number to an empty string and then validates that the TranslateCommand cannot be executed.
    • Name the method TestNoTranslateOnEmptyText.
    • Set the translateViewModel.PhoneNumberText to an empty string.
    • Check the translateViewModel.TranslateCommand.CanExecute method and verify it returns false. You can pass in a null to the parameter for the command.
  2. Run the test from IDE and see if it works.
  3. Show Code

  1. Now write the opposite test. Using the code you already wrote, change the phone number and verify that the command is now executable(it should return true).
  2. Run this test and verify that it functions properly.
  3. Show Code

Test the Dialer in the View Model

  1. Now verify the dialer functionality in the MainViewModel. Define a new test method TestDialerCalls.
  2. In the method, clear out the appViewModel.DialedNumbers collection.
  3. Next, set the translateViewModel.PhoneNumberText to a valid phone number.
  4. Next, invoke the TranslateCommand and the CallCommand on the translateViewModel instances. This will invoke the dialer.
  5. Finally, verify that the dialer was called

    Show Code

  6. Write additional tests to verify that the number was correctly translated, passed into the dialer, and that the DialedNumbers collection has a single dialed number in it.

Run All Tests

As a final step, try running all your tests at once.

  1. Open the Unit Test pad by selecting from the Main Menu View -> Pads and then select Unit Tests.
  2. Select the Run All option from the Unit Test tab to see the results of the Unit Tests.

Summary

Congratulations, you have successfully run and created unit tests on your platform.

Go Back