gpt4 book ai didi

c# - 如何使用最小起订量模拟 ControllerContext?模拟 ControllerContext 时出错

转载 作者:太空狗 更新时间:2023-10-29 20:40:26 24 4
gpt4 key购买 nike

我正在使用 MOQ 框架,我有以下单元测试,它失败并在下面的代码行显示以下错误消息“对象引用未设置为对象的实例”

viewCxt.View.Render(viewCxt, writer);  

谁能指出我正确的方向,为什么这没有通过测试?

[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();
}
}

这是 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)

最佳答案

调试 Mock 上实际需要设置的内容的一个好方法是使用 MockBehavior.Strict 选项创建它。所以,

var mockHttpContext = new Mock<HttpContextBase>();

成为

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

然后,当您的测试需要您尚未设置的 Context 中的某些内容时,您的测试将失败并返回 MockException。您可以稍后恢复为松散选项。

关于c# - 如何使用最小起订量模拟 ControllerContext?模拟 ControllerContext 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6298109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com