Click here to Skip to main content
15,884,052 members
Articles / Web Development / ASP.NET

Implementing Model-View-Presenter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.80/5 (27 votes)
17 Nov 2007CPOL12 min read 129.5K   2.7K   120  
Three implementations of Model-View-Presenter in ASP.NET 2.0.
using System;
using System.Data;
using Model;
using Model.Data.Interfaces;
using Presentation.Interfaces;
using Rhino.Mocks;
using NUnit.Framework;
using Rhino.Mocks.Constraints;

namespace Presentation.Tests
{
	[TestFixture]
	public class ProductsPresenterFixture
	{
		MockRepository mocks = null;
		ProductsPresenter presenter = null;
		IProductsView view = null;
		IDomainDAO<IProduct> model = null;
		int productID;
		int eventProductID;
		IDataReader dataReader = null;

		[SetUp]
		public void SetUp()
		{
			mocks = new MockRepository();
			view = mocks.DynamicMock<IProductsView>();
			model = mocks.DynamicMock<IDomainDAO<IProduct>>();
			productID = 1;
			eventProductID = 0;
			dataReader = mocks.DynamicMock<IDataReader>();

			presenter = new ProductsPresenter(view, model);
		}

		[TearDown]
		public void TestCleanup()
		{
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void NullViewShouldThrowArgumentNullException()
		{
			presenter = new ProductsPresenter(null,model);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void NullModelwShouldThrowArgumentNullException()
		{
			presenter = new ProductsPresenter(view, null);
		}

		[Test]
		public void InitViewShouldSetProductsAndRefreshTimeOnView()
		{
			SetUpInitView();

			presenter.InitView(false);

			mocks.VerifyAll();
		}
		
		[Test]
		public void InitViewShouldAlwaysSetRefreshTimeOnView()
		{
			SetUpInitView();

			presenter.InitView(false);
			presenter.InitView(true);

			mocks.VerifyAll();
		}

		private void SetUpInitView()
		{
			view.SetProducts(dataReader);
			LastCall.Constraints(Is.TypeOf(typeof(IDataReader)) && Is.NotNull());
			SetupResult.For(model.FetchAllByName(true)).Return(dataReader);

			view.SetRefreshTime(DateTime.Now);
			LastCall.Constraints(Is.TypeOf(typeof(DateTime)) && Is.NotNull());

			mocks.ReplayAll();
		}

		[Test]
		public void UpdateProductDetailsShouldSetProductDetailsOnView()
		{
			IProduct product = mocks.DynamicMock<IProduct>();
			SetupResult.For(model.FetchByID(productID.ToString())).Return(product);
			view.SetProductDetails(product);
			LastCall.Constraints(Is.TypeOf(typeof (IProduct)) && Is.NotNull());

			mocks.ReplayAll();

			presenter.UpdateProductDetails(productID.ToString());

			mocks.VerifyAll();
		}

		[Test]
		public void EditProductShouldRaiseRequestProductEditEvent()
		{
			presenter.RequestProductEdit += new EventHandler<SingleValueEventArgs<int>>(RequestProductEditListener);
			
			Assert.AreEqual(0, eventProductID);
			presenter.EditProduct(productID);
			Assert.AreEqual(1, eventProductID);
		}

		private void RequestProductEditListener(object sender, SingleValueEventArgs<int> e)
		{
			eventProductID = e.Value;
		}
	}
}

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 States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions