In this chapter, we modify the Unleashed Blog application so that it supports retrieving archived blog entries. In other words, we modify the blog application so that it supports the user stories displayed in Figure 18.1.
We need to create a new Archive
controller; however, because we practice test-driven development, we start with the tests.
We need to add two types of tests. First, we need to create tests that verify that the Archive
controller behaves in the way that we expect. We also need to create tests for the routes that we define in the Global.asax file.
According to a careful reading of the napkin, we need to retrieve archived blog entries by date or name. For example, any of the following browser requests should return a list of blog entries:
/Archive/2010—Returns all blog entries from the year 2010
/Archive/2010/12—Returns all blog entries from December 2010
/Archive/2010/12/25—Returns all blog entries from Christmas 2010
/Archive/2010/12/25/Aliens-Attack—Returns the blog entry with the name Aliens Attack from Christmas 2010
We can express all these requirements with the set of tests in Listing 18.1.
For example, the first test in Listing 18.1 verifies that the Archive
controller returns a list of blog entries that match a particular year. The test has an Arrange
, Act
, and Assert
section.
In the Arrange
section, an instance of the fake repository, Blog
controller, and Archive
controller are created. Three blog entries are created. Two of the blog entries have the year 2010 for their publication date, and one of the blog entries has the year 2011.
Next, in the Act
section, the Archive
controller Index()
action is invoked with the value 2010 for the year.
Finally, in the Assert
section, an assertion is made that two blog entries are returned from the Index()
action. In other words, only the two blog entries with a publication date of 2010 should be returned, and the blog entry with a publication date of 2011 should not be returned.
When you create the tests in Listing 18.1, Visual Studio displays a red squiggly error message below every reference to the ArchiveController
class. This class does not yet exist. We can’t compile the tests because the Archive
controller does not exist.
Before we do anything else, we should start by writing just enough application code to get our solution back into a state in which we can do a successful build. To build our application, we need to create the controller
class in Listing 18.1A.
The Archive
controller in Listing 18.1A gives us enough code to successfully compile our application. However, if we run our tests, the tests fail (see Figure 18.1A). That’s good because we always want to start with a failing test. (We need to see the red before we see the green).
Because we have a set of failing tests, we can allow ourselves to write some application code. An updated Archive
controller is contained in Listing 18.2.
The Archive
controller exposes one action named Index()
. The Index()
action calls the ListBlogEntries()
method on the repository class. The BlogRepositoryBase
class in Listing 18.3 has been updated to include this new method.
The ListBlogEntries()
method in Listing 18.1 contains the application logic required to filter the list of blog entries by year, month, day, and name. This logic is used by both the fake repository and the Entity Framework repository.
After making these changes, the new set of Archive
controller tests pass (see Figure 18.2).
To use the Archive
controller, we need to add new routes to the route table defined in the Global.asax file. Right now, the Global.asax file contains a single Default
route that maps incoming browser requests to the Blog
controller. We need to map the right requests to the Archive
controller.
Because our route table starts to become complicated, we should build tests. We want to make sure that the right requests match the right route. We can use a set of tests in Listing 18.4 to test our routes.
The test class in Listing 18.4 contains the following five tests:
DefaultRoute
—Verifies that a request for ~/
matches the Default
route.ArchiveYear
—Verifies that a request for ~/Archive/2008
matches the ArchiveYear
route.ArchiveYearMonth
—Verifies that a request for ~/Archive/2008/12
matches the ArchiveYearMonth
route.ArchiveYearMonthDay
—Verifies that a request for ~/Archive/2008/12/25
matches the ArchiveYearMonthDay
route.ArchiveYearMonthDayName
—Verifies that a request for ~/Archive/2008/12/25/Test
matches the ArchiveYearMonthDayName
route.Each route passes different information to the controller. For example, the ArchiveYearMonthDay
route passes values for the year, month, and day parameters to the controller.
Before you can use the test class in Listing 18.4, you need to add references to the MvcFakes.dll and the RouteDebugger.dll assemblies:
• Add a reference to the MvcFakes.dll assembly to the UnleashedBlog.Tests project by selecting the UnleashedBlog.Tests project. Select the Project, Add Reference menu option and browse to the MvcFakes.dll assembly in the MvcFakes project in the sample code (available on the book’s website, www.informit.com/title/9780672329982) (see Figure 18.3).
• Add a reference to the RouteDebugger.dll assembly to both the UnleashedBlog project and the UnleashedBlog.Tests project. Select the Project, Add Reference menu option and browse to the RouteDebugger.dll assembly in the RouteDebugger project in the sample code on the website (see Figure 18.4).
After you create the route tests, and you add the necessary references, you can run the tests by pressing the keyboard combination Ctrl+R, A. Because we have not added the necessary routes to the Global.asax file, several of the tests should fail (see Figure 18.5).
In the next section, we modify the Unleashed Blog application so that it contains the necessary routes to pass these tests.
We need to add the right routes to the route table created in the Global.asax file. We need to pass the right parameters, such as the year, month, day, and name, to the controller that matches the route.
The modified Global.asax file is contained in Listing 18.5.
If you update the Global.asax file with the new routes and you rerun the tests in your test project by pressing Ctrl+R, A, all the tests pass (see Figure 18.6).
You can try out the Archive
controller by adding a new view to the UnleashedBlog project. Open the Archive
controller, right-click the Index()
action, and select the menu option Add View. Create a new strongly typed view with a BlogEntry
view class and a List
view content (see Figure 18.7).
After you add the Index
view for the Archive
controller, you can invoke the Archive
controller by using a URL like the following:
/Archive/2008
Invoking the Archive
controller with the URL returns all the blog entries from the year 2008 (see Figure 18.8).
In this chapter, we added support to the Unleashed Blog application for retrieving archived blog entries. First, we created a new set of tests to test the Archive
controller. We created the Archive
controller and modified the blog
repository to satisfy these tests.
Next, we created a new set of tests to tests our routes. To use these tests, we need to add references to the MvcFakes.dll assembly and the RouteDebugger.dll assembly.
Finally, we tested out the new Archive
controller by adding a new view to our blog application. We can filter archived blog entries by year, month, day, or name.
18.191.240.127