简体   繁体   中英

How to mock the ControllerContext using moq? ERROR while mocking the ControllerContext

I am using the MOQ Framework and I have the following Unit test and it is failing with the below error message “Object reference not set to an instance of an object” At the below line of code

viewCxt.View.Render(viewCxt, writer);  

Could anyone point me in the right direction please as to why this is not passing the test?

[Test]
public void can_call_PopulateBankTransactionWorkQueueViewInTransactionMetaDataAjaxResponseObject()
{

    var transactionMetaData = new TransactionMetaDataDTO() { TransactionId = "1", FileId = "1", LockboxNumber = "0402020", DepositDate = "04.26.2011", BatchId = "1" };


    var request = new Mock<HttpRequestBase>();
    request.Setup(r => r.HttpMethod).Returns("GET");
    var mockHttpContext = new Mock<HttpContextBase>();
    mockHttpContext.Setup(c => c.Request).Returns(request.Object);

    var controllerContext = new ControllerContext(mockHttpContext.Object, new Mock<RouteData>().Object, new Mock<ControllerBase>().Object); 

    var checkWorkQueueController = new CheckWorkQueueController(
          activeDirectorySecurityManager.Object,  businessObjectAdapter, httpRequestObjectHelper.Object, invoiceRepos.Object,  new HtmlHelpers());

    checkWorkQueueController.ControllerContext = controllerContext;
    Assert.DoesNotThrow(() => checkWorkQueueController.PopulateBatchTreeSelectorViewInTransactionMetaDataAjaxResponseObject(transactionMetaData));
}

internal void PopulateBatchTreeSelectorViewInTransactionMetaDataAjaxResponseObject(TransactionMetaDataDTO transactionMetaDataDTO)
{

    var checkWorkQueueViewModel = new CheckWorkQueueViewModel(securityManager, businessObjectAdapter);
    SetActiveFileAndLockbox(transactionMetaDataDTO, checkWorkQueueViewModel, transactionMetaDataDTO.FileId, transactionMetaDataDTO.LockboxNumber);
    transactionMetaDataDTO.BatchTreeSelectorView = htmlHelpers.RenderViewToString(ApplicationConstants.CheckWorkQueueViewPath + ApplicationConstants.BatchTreeSelectorViewFileName, this, checkWorkQueueViewModel);
}

public string RenderViewToString<T>(string viewPath, ControllerBase controller, T model)
{
        controller.ViewData.Model = model;
        using (var writer = new StringWriter())
        {
            var view = new WebFormView(viewPath);
            var vdd = new ViewDataDictionary<T>(model);
            var viewCxt = new ViewContext(controller.ControllerContext, view, vdd, new TempDataDictionary(), writer);
            viewCxt.View.Render(viewCxt, writer);  //ERROR throwing here
            return writer.ToString();
        }
}

Here is the StackTrace:

at System.Web.VirtualPath.GetCacheKey()
at System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath(VirtualPath virtualPath, Boolean& keyFromVPP)
at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType)
at System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer)

a good way to debug what actually needs to be setup on the Mock is to create it with the MockBehavior.Strict option. So,

var mockHttpContext = new Mock<HttpContextBase>();

becomes

var mockHttpContext = new Mock<HttpContextBase>(MockBehavior.Strict);

Then, your test will fail with a MockException when it requires something from the Context that you haven't set up. You can revert to the Loose option later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM