MichaelPuleio wrote:
To be clear, the HttpContext BillKrat uses above is a Microsoft.Practices.CompositeWeb.Web.HttpContext, which implements Microsoft.Practices.CompositeWeb.Interfaces.IHttpContext.
I would use the interface, where he uses the derived class, for looser coupling and mockability.
Yep, looks like I saved my edit 1 minute before you posted your rcomment so they may not get that I was using
HttpContext versus
IHttpcontext (I caught this while writing a unit test using pbolduc's code from the
http://www.codeplex.com/websf/Thread/View.aspx?ThreadId=24600 message thread.
A variation of his test (to support the controller holding the httpcontext) follows:
using System;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections.Generic;
using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.CompositeWeb.Services;
using Microsoft.Practices.CompositeWeb.Interfaces;
using UserInfo.Views;
using UserInfo.Tests.Mocks;
using Rhino.Mocks;
// This little snippet was borrowed from the ObjectBuilder's unit test - allows us
// to use the default VS2008 test with NUnit.
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif
namespace UserInfo.Tests
{
/// <summary>
/// Summary description for UserInfoModuleInitializerFixture
/// </summary>
[TestClass]
public class UserInfoModuleControllerFixture
{
private MockRepository mocks;
public UserInfoModuleControllerFixture()
{
}
[TestInitialize]
public void Setup()
{
mocks = new MockRepository();
}
[TestMethod]
public void ViewLoadedSetsCache()
{
// mock up our IHttpContextLocatorService
IHttpContextLocatorService httpContextLocatorService = mocks.DynamicMock<IHttpContextLocatorService>();
// wire up the mock repository
IHttpContext httpContext = mocks.DynamicMock<IHttpContext>();
SetupResult.For(httpContextLocatorService.GetCurrentContext()).Return(httpContext);
// we'll just use HttpRuntime.Cache which is available outside ASP.NET
Cache cache = HttpRuntime.Cache;
SetupResult.For(httpContext.Cache).Return(cache);
mocks.ReplayAll(); //
MockUserInfoController controller = new MockUserInfoController();
controller.Context = httpContext;
DefaultViewPresenter presenter = new DefaultViewPresenter(controller);
presenter.View = mocks.DynamicMock<IDefaultView>();
Assert.AreEqual(0, cache.Count);
Assert.IsNull(cache["DefaultViewPresenter_NameKey"]);
presenter.OnViewLoaded();
Assert.AreEqual(1, cache.Count);
Assert.IsNotNull(cache["DefaultViewPresenter_NameKey"]);
}
}
}
DefaultViewPresenter
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
namespace UserInfo.Views
{
public class DefaultViewPresenter : Presenter<IDefaultView>
{
private IUserInfoController _controller;
public DefaultViewPresenter([CreateNew] IUserInfoController controller)
{
this._controller = controller;
}
public override void OnViewLoaded()
{
string name = _controller.Context.Cache["DefaultViewPresenter_NameKey"] as string;
if (name == null)
{
name = "Phil"; // this would come from some service or something
_controller.Context.Cache["DefaultViewPresenter_NameKey"] = name;
}
base.OnViewLoaded();
}
public string GetQueryStringValue(string queryName)
{
string queryString = _controller.Context.Request.QueryString[queryName];
return queryString;
}
}
}
IUserInfoController interface
using System;
using Microsoft.Practices.CompositeWeb.Web;
using Microsoft.Practices.CompositeWeb.Interfaces;
namespace UserInfo
{
public interface IUserInfoController
{
IHttpContext Context { get; set; }
}
}