Click here to Skip to main content
15,895,656 members
Articles / Operating Systems / Windows

MVP - A Basic Demonstration of its Power

Rate me:
Please Sign up or sign in to vote.
3.46/5 (4 votes)
31 Aug 2006CPOL5 min read 41K   381   14  
A basic demonstration on how to build a Unit Testable, Web and Windows Login UI
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Heynemann.LoginSample.UI;

namespace Heynemann.LoginSample.UnitTests {
    /// <summary>
    /// Summary description for LoginUITest
    /// </summary>
    [TestClass]
    public class LoginUITest {
        public LoginUITest() {
        }

        private ILoginForm view;
        private LoginFormPresenter presenter;
        
        [TestInitialize()]
        public void MyTestInitialize() {
            //Create a new view (the one that simulates a form)
            view = new Mocks.LoginFormMock();
            //Create the presenter that will be responsible for
            //the behavior of the view.
            presenter = new LoginFormPresenter(view);
            //Initialize the presenter.
            presenter.InitializeView(ViewInitialization.FirstTime);
        }

        [TestMethod]
        public void TestLoginUI() {
            //values to test against.
            string targetUser = "heynemann";
            string targetPass = "MVProx!";

            //Set the view values to the values I want to use.
            view.Username = targetUser;
            view.Password = targetPass;
            
            //Before invoking the event, I make sure the view
            //retained the values I passed in.
            Assert.AreEqual<string>(targetUser, view.Username);
            Assert.AreEqual<string>(targetPass, view.Password);
            
            //This method is used to simulate a button click.
            ((Mocks.LoginFormMock)view).RaiseLoginButtonClickEvent();

            //If the Presenter handled the click event 
            //the user should be logged by now.
            Assert.AreEqual<bool>(true, view.IsLoggedIn);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
Bernardo Heynemann is a senior developer at ThoughtWorks UK in London. He is really into Visual Studio 2008, LINQ and ASP.Net MVC. He's also chairman of Stormwind Project (http://www.stormwindproject.org). He can be found at his blog at http://blogs.manicprogrammer.com/heynemann.

Comments and Discussions