Unit Testing
All the Navigational concepts in the above sections, apart from the Controls and
ASP.NET Ajax History, work outside of a Web environment and hence can be Unit Tested e.g. using Visual Studio Unit Tests.
Sample Web Site
The first Unit Tests will check the State Information configuration. Beging by adding a new Unit Test Project to the solution and for this new project:
- Manually add a reference to Navigation.dll (the NuGet installation is only suitable for Web projects)
- Add a New Basic Unit Test class called StateInfoTest.
- Add a Solution Folder - right click the Solution and select Add -> New Solution Folder
- Add Test Settings to the Solution Folder – right click the Solution Folder and select Add New Item, select Test Settings and give it a name
- Open the Test Settings, select Deployment, check the Enable deployment checkbox, select Add File and Browse to the StateInfo.config file in the Web Project (make sure to show All files in the browser dialog)
- From the main menu select Test -> Test Settings -> Select Test Settings File and Browse to the Test Settings file just added
- Add an App.config file and configure as shown below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Navigation">
<section name="StateInfo" type="Navigation.StateInfoSectionHandler, Navigation"/>
</sectionGroup>
</configSections>
<Navigation>
<StateInfo configSource="StateInfo.config">
</StateInfo>
</Navigation>
</configuration>
Add a Test Method to StateInfoTest called NavigatePersonTest as shown in below (making sure to add a using to the Navigation namespace). This test checks that Navigating to the Person Dialog results in the Listing State, and it will pass when run.
[TestMethod]
public void NavigatePersonTest()
{
StateController.Navigate("Person");
Assert.AreEqual("Listing", StateContext.State.Key);
}
To test the Select Transition from Listing, run the passing test shown below.
[TestMethod]
public void NavigateListingSelectTest()
{
StateController.Navigate("Person");
StateController.Navigate("Select");
Assert.AreEqual("Details", StateContext.State.Key);
}
The next Unit Test will verify the Search method on the PersonSearch class. In the Test project add a reference to both the Web project and to the System.Web library. Next, add a New Basic Unit Test class called PersonSearchTest and to it add a Test Method called SearchTest and using statements to the Navigation and System.Linq namespaces. Call the Search method on the PersonSearch class and check the number of items returned is 10 and the total count in StateContext Data is 12 as shown below. This test will pass when run. The remaining PersonSearch tests are left as an exercise.
[TestMethod]
public void SearchTest()
{
var actual = new PersonSearch().Search(null, null, null, 0, 10);
Assert.AreEqual(10, actual.ToList().Count);
Assert.AreEqual(12, StateContext.Data["totalRowCount"]);
}