Here is the first part of my controller code:
public class ControlMController : Controller
{
IControlMService _controlMservice;
public IList<User> Users
{
get
{
if (System.Web.HttpContext.Current.Cache["users"] == null)
{
System.Web.HttpContext.Current.Cache["users"] = _controlMservice.GetUsers();
}
return (IList<User>)System.Web.HttpContext.Current.Cache["users"];
}
}
public ControlMController(IControlMService controlMservice)
{
this._controlMservice = controlMservice;
var users = Users;
ViewData["Users"] = users;
ViewData["jqSelectUsers"] = string.Join(";", users.Select(x => x.UserID + ":" + x.Name).ToArray());
}
I'm trying to test it, and because i'm caching using the HttpContext, i'm struggling with null reference exceptions. I've tried using MvcContrib.TestHelper;
here is my sample test...
[TestMethod]
public void EventDetails_Returns_view_with_correct_event()
{
var builder = new TestControllerBuilder();
var controller = builder.CreateController<ControlMController>(
new ControlMService(
new MockControlMRepository()
));
var view = (controller.EventDetails(1) as ViewResult);
Assert.AreEqual(1, (view.ViewData.Model as Event).EventId);
}
(I haven't quite got round to using DI for my tests!
I'm still getting the same null reference exception when the code hits the httpcontext:
Error 1 TestCase 'SupportTool.Tests.Services.ControlM.ControlMControllerTests.EventDetails_Returns_view_with_correct_event'
failed: System.NullReferenceException: Object reference not set to an instance of an object.
at SupportTool.web.Controllers.ControlMController.get_Users()
Any ideas?