Duration
20 minutes
Lab Goals
The primary goal of this exercise is to create and run unit tests.
- Add a new Unit Test project to the solution
- Write tests for the Phoneword app
Steps
Create the Unit Test Project
- Open the Start solution in the Exercise 2 folder.
- Add a new Visual C# > Class Library (.NET Framework) project and name it Phoneword.UnitTests.Core.
- Install the latest NUnit and NUnit3TestAdapter NuGet packages into this new project.
- Add a new NUnit Library Project and name it Phoneword.UnitTests.Core.
- 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
- Right click on the References for the test project and add a project reference to the Phoneword.Core project.
- In the new class library project, rename the default class file to TestTranslation.cs.
-
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() { } } } - In the test project, rename the boilerplate test file (likely named
Test.cs) toTestTranslation.cs - Add
using Phoneword.Core;to the file so we can get to thePhonewordTranslatorclass.
Create a Unit Test
- Rename the existing
TestCasemethod toTestXamarinPhoneNumber - Add code to the
TestXamarinPhoneNumbermethod to perform the following test:- Invoke the
PhonewordTranslator.ToNumbermethod with the phone number "1-855-XAMARIN" - Verify that the returning number is "1-855-9262746" using the
Assertclass.
- Invoke the
Create a testable mock IDialer
- Create a mock implementation of
IDialerwhich captures the value of the passed number and tracks whether theDialmethod was called. You can use the following implementation:
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.
- Create a new unit test by adding a new C# class named TestPhoneTranslateViewModel the test project.
- Copy in the
TestFixturecode used in the previous test. - Add
using Phoneword.Core;to the namespace references.
Setup the Test Class
- We need to setup the test fixture by creating a new
MainViewModelwhich will invoke the dialer and a new instance of thePhoneTranslateViewModelwhich 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
MockDialerand save it off in a field. - Name the
MainViewModelfieldappViewModeland thePhoneTranslateViewModelfieldtranslateViewModel. - Pass in the
MockDialerinstance into yourMainViewModelconstructor. - Pass the
MainViewModelinto thePhoneTranslateViewModelconstructor.
- Use a
Test the Dial Button
- 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
TranslateCommandcannot be executed.- Name the method
TestNoTranslateOnEmptyText. - Set the
translateViewModel.PhoneNumberTextto an empty string. - Check the
translateViewModel.TranslateCommand.CanExecutemethod and verify it returnsfalse. You can pass in anullto the parameter for the command.
- Name the method
- Run the test from IDE and see if it works.
- 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).
- Run this test and verify that it functions properly.
Test the Dialer in the View Model
- Now verify the dialer functionality in the
MainViewModel. Define a new test methodTestDialerCalls. - In the method, clear out the
appViewModel.DialedNumberscollection. - Next, set the
translateViewModel.PhoneNumberTextto a valid phone number. - Next, invoke the
TranslateCommandand theCallCommandon thetranslateViewModelinstances. This will invoke the dialer. - Finally, verify that the dialer was called
- Write additional tests to verify that the number was correctly translated, passed into the dialer, and that the
DialedNumberscollection has a single dialed number in it.
Run All Tests
As a final step, try running all your tests at once.
- Open the Unit Test pad by selecting from the Main Menu View -> Pads and then select Unit Tests.
- 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.