LINQBugging MVC Controllers?

I've used LINQPad several times to do what Matthew MacSuga refers to as LINQBugging WinForms/WPF (See link on linqpad.net).

I've also had limited success testing/debugging utility components of MVC controllers, however, so far, I haven't managed to LINQBug the main parts of an MVC controller as I haven't (yet) managed to initialize HttpSessionState, HttpContext, etc., outside of a web server!

The 2 questions I have are, firstly, has anyone managed to do this?

Alternatively, is there some was to invoke LINQPad from an application, e.g. call it from the index page of a controller, and have it start on a separate thread with access the the running process?

I realize that LINQPad wasn't really intended to be a debugger, but it has so many advantages over using the standard Visual Studio debugger or going through the edit/compile/test cycle on a large solution!


  • This is definitely possible, and made easiest if you include a mocking library like Moq (available as a nuget package). Simply add the reference to the assemblies for MVC, your application and Moq to your LINQPad query, then use code similar to the following to mock out the needed pieces for your tests.

    // Arrange var session = new Mock<System.Web.HttpSessionStateBase>(); session.SetupProperty(s => s.Keys); var context = new Mock<System.Web.HttpContextBase>(); context.SetupGet(c => c.Session).Returns(session.Object); var home = new Mvc4Application1.Controllers.HomeController { ControllerContext = new ControllerContext { HttpContext = context.Object } }; // Action var result = home.Index(); // Assert (result != null);

    It should go without saying that this would generally be better encapsulated in an nunit/xunit/etc. test assembly. This is shown as a response to is it possible and how to do it.
Sign In or Register to comment.