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. Begin by adding a new Unit Test Project to the solution and for this new project:
  1. Manually add a reference to Navigation.dll (the NuGet installation is only suitable for Web projects)
  2. Add a New Unit Test class called StateInfoTest.
  3. Right click the Test project and select Add -> Existing Item... Browse to the StateInfo.config file in the Web Project (make sure to show All files in the browser dialog) and select Add -> Add As Link
  4. In the Properties window of the linked StateInfo.config file set Copy to Output Directory to Copy Always
  5. 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);
}
NavigationLinks generated in a Test environment are identical to those generated in a Web environment. So the next Unit Tests will consist of Url verification. The code below tests the Url for Navigating to the Person Dialog matches the configured route.

[TestMethod]
public void NavigatePersonLinkTest()
{
	string link = StateController.GetNavigationLink("Person");
	Assert.AreEqual("/List", link);
}
Route parameters can be tested by passing NavigationData in the call to GetNavigationLink. Below is a test verifying the Url is constructed as expected when passing startRowIndex.

[TestMethod]
public void NavigatePersonStartRowIndexLinkTest()
{
	NavigationData data = new NavigationData();
	data["startRowIndex"] = 10;
	string link = StateController.GetNavigationLink("Person", data);
	Assert.AreEqual("/List/10", link);
}
A similar test checking maximumRows, the second route parameter, is shown below.

[TestMethod]
public void NavigatePersonMaximumRowsLinkTest()
{
	NavigationData data = new NavigationData();
	data["maximumRows"] = 20;
	string link = StateController.GetNavigationLink("Person", data);
	Assert.AreEqual("/List/0/20", link);
}
Having trackCrumbTrail set to false on the Listing State makes these Url tests simpler because it means there will not be any query string parameters appended by the Navigation framework. Similar tests for the Details State would need to allow for these.

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 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"]);
}

Last edited Feb 15, 2014 at 4:19 PM by GrahamMendick, version 4