Lab 3: Adding Views and Unit Testing

来源:互联网 发布:vb 强制声明 编辑:程序博客网 时间:2024/04/30 19:22

 

Purpose

Estimated time to complete this lab: 30 minutes

You can use the View-Presenter pattern to separate the responsibilities for the visual display and the event handling behavior into different classes. Separation of these concerns leads to simplified classes, allows reuse of presentation logic, and helps you test the presentation logic without a visual user interface.

In this exercise, you will add a new view to the EFT module using the View-Presenter pattern. This view will display the last transfer date when it loads, as shown in Figure 1. You will also implement unit tests for the presenter to test the presentation logic.

For more information about the View-Presenter pattern, see “View-Presenter” in Web Client Software Factory Help.

 

Figure 1
View that shows last transfer date (when applicable)

Preparation

Before proceeding with this lab, you must install and configure the prerequisite software. For more information, see Web Client Software Factory Hands-On Labs.

Open the solution for the previous lab (either the one that you created or the end solution for that lab.) If you use the provided end solution, you must enable the guidance package for that solution.

To enable the Web Client Development guidance package with the Guidance Package Manager

1.       Using Visual Studio, open the solution.

2.       On the Tools menu, click Guidance Package Manager.

3.       In the Guidance Package Manager dialog box, click Enable / Disable Packages.

4.       In the Enable and Disable Packages dialog box, select the Web Client Development check box.

5.       Click OK.

Procedures

This lab includes the following tasks:

·         Task 1: Create a New View in the EFT Module

·         Task 2: Create Unit Tests for the Presenter

·         Task 3: Run the Unit Tests

·         Task 4: Implement the Presenter

·         Task 5: Add the View to the Site Map

The next sections describe each of these tasks.

Task 1: Create a New View in the EFT Module

The Web Client Software Factory includes a recipe named Add View (with presenter), which creates an implementation of the View-Presenter pattern for you. In this task, you will create a new view using the recipe.

To create a new view in the EFT module

1.       Create a new view named LastTransferView. To do this, right-click the EFT folder inside DevelopmentWebsite site, point to Web Client Factory, and then click Add View (with presenter) (C#). The recipe wizard appears, as shown in Figure 2.

 

Figure 2
Add View (with presenter) recipe wizard

2.       In the View name box, type LastTransferView, and then click Finish.

The Add View (with presenter) recipe will add the following elements to your solution (as illustrated in Figure 3):

·   A view interface definition. The Views folder of the module project will contain ILastTransferView.cs. By defining an interface for the view, you can easily replace it with a mock view when writing unit tests for the presenter.

·   A presenter class. The Views folder of the module project will contain LastTransferViewPresenter.cs. The presenter will contain the logic to respond to events, and in turn, will manipulate the state of the view.

·   A view implementation. The Web site folder of the module project will contain LastTransferView.aspx and LastTransferView.aspx.cs. The view implementation is responsible for managing the controls on the page and for forwarding user events to the presenter.

·   Unit tests for the presenter. This includes a mock view implementation. (If you do not create a unit test project for your business module, the recipe does not create the unit tests for the presenter.)

 

Figure 3
Solution Explorer view of new elements in the Views folder

The LastTransferView view will display the date of the last transfer processed. If there has not been at least one transfer processed, it will display a message instead of the date. To support both scenarios, the view will expose the following two properties that the presenter will set:

·   ShowLastTransferDate. This property indicates to the view whether to show the date of the last transfer.

·   LastTransferDate. This property indicates the date of the last transfer.

In the next steps, you will implement these properties.

3.       Open the file ILastTransferView.cs, and then use the following code to define the view interface.

public interface ILastTransferView

{

    bool ShowLastTransferDate { set; }

    DateTime LastTransferDate { set; }

}

4.       Add two Panel controls to the LastTransferView view. One panel will display the last transfer date, and the other panel will display a message that indicates there are no prior transfers. To do this, open the file EFT/LastTransferView.aspx in Source view, and then replace the asp:Content markup with the following code.

<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" runat="Server">

  <h1>Last Transfer</h1>

  <asp:Panel runat="server" ID="LastTransferPanel">

    <strong>Last transfer date: </strong>

    <asp:Label runat="server" Text="" ID="TransferDateLabel" />

  </asp:Panel>

  <asp:Panel Visible="false" runat="server" ID="NoTransfersPanel">

    <p>Sorry, there are no transfers.</p>

  </asp:Panel>

</asp:Content>

5.       In the file LastTransferView.aspx.cs, add the following code inside the class body to implement the view’s interface.

#region ILastTransferView Members

 

public bool ShowLastTransferDate

{

    set

    {

        LastTransferPanel.Visible = value;

        NoTransfersPanel.Visible = !value;

    }

}

 

public DateTime LastTransferDate

{

    set

    {

        TransferDateLabel.Text = String.Format("{0} {1}", value.ToShortDateString(), value.ToShortTimeString());

    }

}

 

#endregion

Task 2: Create Unit Tests for the Presenter

When the LastTransferView view loads, it displays either the last transfer date (when there has been at least one transfer performed previously), or it displays a message that indicates there has been no prior transfer. In this task, you will write unit tests to verify this behavior.

To write the unit tests for the presenter, you will not use the real implementations of the view and the controller classes. Instead, you will use mock objects so you can test the presenter in isolation.

To create unit tests

1.       The Add View (with presenter) recipe created a mock view named MockLastTransferView (located in the LastTransferViewPresenterFixture.cs file in the EFT.Tests project) that you can use to test the presenter. Update this class to implement the interface ILastTransferView, as shown in the following code.

class MockLastTransferView : ILastTransferView

{

    private bool _showLastTransferDate = false;

    private DateTime _lastTransferDate;

 

    #region ILastTransferView Members

 

    public bool ShowLastTransferDate

    {

        set { _showLastTransferDate = value; }

        get { return _showLastTransferDate; }

    }

 

    public DateTime LastTransferDate

    {

        set { _lastTransferDate = value; }

        get { return _lastTransferDate; }

    }

 

    #endregion

}

2.       In the Mocks folder of the EFT.Tests project, open the file MockEFTController.cs, and then add the following public field to the MockEFTController class.

public DateTime TransferDate;

3.       Use the following code to override the implementation of the GetLastTransferDate method.

public override DateTime GetLastTransferDate()

{

    return TransferDate;

}

4.       Open the file LastTransferViewPresenterFixture.cs, and then add the following using statement at the top of the file.

using GlobalBank.EFT.Tests.Mocks;

5.       Create a test that verifies that the last transfer date is shown when there has been a prior transfer. To do this, add the following code to the LastTransferViewPresenterFixture class.

[TestMethod]

public void OnLoadSetsAndShowsLastTransferDateInView()

{

    MockEFTController controller = new MockEFTController();

    controller.TransferDate = new DateTime(2000, 1, 1);

    LastTransferViewPresenter presenter = new LastTransferViewPresenter(controller);

    MockLastTransferView view = new MockLastTransferView();

    presenter.View = view;

 

    presenter.OnViewLoaded();

 

    Assert.AreEqual<DateTime>(new DateTime(2000, 1, 1), view.LastTransferDate);

    Assert.IsTrue(view.ShowLastTransferDate);

}

 

6.       Create a test that verifies that no transfer date is shown when there has not been a prior transfer. To do this, add the following code to the test fixture class.

[TestMethod]

public void OnLoadSetsNotShowLastTransferDateInViewIfDateIsMinValue()

{

    MockEFTController controller = new MockEFTController();

    controller.TransferDate = DateTime.MinValue;

    LastTransferViewPresenter presenter = new LastTransferViewPresenter(controller);

    MockLastTransferView view = new MockLastTransferView();

    view.ShowLastTransferDate = true;

    presenter.View = view;

 

    presenter.OnViewLoaded();

 

    Assert.IsFalse(view.ShowLastTransferDate);

}

Task 3: Run the Unit Tests

In this task, you will run the unit tests you created in the previous task.

To run the unit tests

1.       If you run the tests now, there will be compilation errors. This happens because the presenter does not have a constructor that receives an EFTController instance as a parameter. Before running the tests, you need create the constructor. To do this, open the file LastTransferViewPresenter.cs (located in the Views folder of the EFT project), and then uncomment the constructor generated by the Add View (with presenter) recipe.

private EFTController _controller;

 

public LastTransferViewPresenter([CreateNew] EFTController controller)

{

    _controller = controller;

}

Note: You use the CreateNew attribute to indicate to ObjectBuilder that it has to inject a new instance of the EFTController class when instantiating the presenter.

2.       Use the Test Manager to run the tests. To do this, perform the following steps:

a. In Solution Explorer, click EFT.Tests.

b.      On the Test menu, click Start Selected Tests Project without Debugger (or press CTRL+SHIFT+X).

The results will display in the Test Results window.

You should see that the tests you created in the previous task fail. They fail because the presenter is not yet implemented, so it neither passes the last transfer date to the view nor does it set the ShowLastTransferDate property.

Task 4: Implement the Presenter

In this task, you will implement the presenter to make the unit tests pass.

To implement the presenter

1.       Implement the OnViewLoaded method. To do this, add the following bold code to the method’s body.

public override void OnViewLoaded()

{

    DateTime lastTransferDate = _controller.GetLastTransferDate();

    if (lastTransferDate.Equals(DateTime.MinValue))

    {

        View.ShowLastTransferDate = false;

    }

    else

    {

        View.ShowLastTransferDate = true;

        View.LastTransferDate = lastTransferDate;

    }

}

2.       Run the unit tests. All tests should pass.

Task 5: Add the View to the Site Map

Business modules register site map nodes to expose their features to the Web site. To do this, you use ISiteMapBuilderService. The ISiteMapBuilderService service is provided by the Composite Web Application Block; it uses the site map nodes of each module loaded in the application to create the full site map. The Composite Web Application Block also provides a custom site map data provider, ModuleSiteMapProvider, that consumes the site map. You can use any ASP.NET navigation control (for example, the Menu control or the TreeView control) to display the site map in your Web site user interface.

To add a view to the site map, you create a new SiteMapNodeInfo instance (which represents a site map node) and add it to the ISiteMapBuilderService service using the AddNode method. You implement this code in the RegisterSiteMapInformation method of your module initialization class that is created by the Add Business Module recipe.

For more information about adding views to the site map, see “How to: Add Module Pages to the Site Map” in Web Client Software Factory Help.

In this task, you will add the LastTransferView view to the Web site’s site map.

To add the view to the site map

1.       In the root of the EFT project, open the EFTModuleInitializer.cs file. This is the module initialization class of the module.

2.       Add the LastTransferView view to the Web site’s site map. To do this, paste the following code inside the RegisterSiteMapInformation method.

SiteMapNodeInfo transfersViewNode = new SiteMapNodeInfo("LastTransferView", "~/EFT/LastTransferView.aspx", "Transfers");

 

 siteMapBuilderService.AddNode(transfersViewNode, moduleNode);

Verification

In this section, you will verify that you implemented the LastTransferView view correctly.

To verify you implemented the view correctly

1.       Build and run the application.

2.       Expand the EFT site map node.

3.       Click the Transfers node. You should see the LastTransferView view, as shown in Figure 4.

 

Figure 4
LastTransferView view