简体   繁体   English

为什么 Razor 页面处理程序返回类型的 PartialViewResult 的单元测试失败?

[英]Why does unit test for Razor Page handler return type of PartialViewResult fail?

I am using Core 3.1 Xunit to test a Razor Page in an application, with Moq to mock any services.我正在使用 Core 3.1 Xunit 在应用程序中测试 Razor 页面,并使用 Moq 来模拟任何服务。 When running a test on a handler of PageModel to test if it returns PartialViewResult type I get the following exception:PageModel的处理程序上运行测试以测试它是否返回PartialViewResult类型时,我得到以下异常:

System.NullReferenceException : Object reference not set to an instance of an object.

The PageModel handler looks like this, with Application as a property on PageModel : PageModel处理程序如下所示,其中Application作为PageModel上的一个属性:

public PartialViewResult OnGetApplicationAddedModalPartial()
{
    return Partial("_ApplicationAddedModalPartial", Application);
}

And this is the failing test for it:这是它的失败测试:

[Fact]
public void OnGetApplicationAddedModalPartial_WhenCalled_ShouldReturPartialViewResultType()
{
    //Arrange
    _sut.Application = It.IsAny<ApplicationModel>();

    //Act
    var result = _sut.OnGetApplicationAddedModalPartial();

    //Assert
    Assert.IsType<PartialViewResult>(result);
}

What's curious is that I have another handler in that PageModel that I successfully test to see if the return is of JasonResult type.奇怪的是,我在该PageModel中有另一个处理程序,我成功测试了它以查看返回是否为JasonResult类型。 Additionally, I have controllers in this application of which some actions also return a PartialViewResult type that I successfully test for.此外,我在此应用程序中有一些控制器,其中一些操作还返回我成功测试的PartialViewResult类型。

What is special about a handler of return type PartialViewResult on a PageModel and how can I create a test to ensure that it returns PartialViewResult ? PageModel上返回类型为PartialViewResult的处理程序有什么特别之处,如何创建测试以确保它返回PartialViewResult

When testing a PartialViewResult , you need to instantiate a number of properties of the PageModel as part of your setup: https://github.com/dotnet/AspNetCore.Docs/issues/24312测试PartialViewResult时,您需要实例化PageModel的许多属性作为设置的一部分: https://github.com/dotnet/AspNetCore.Docs/issues/24312

// Arrange
var httpContext = new DefaultHttpContext();
var modelState = new ModelStateDictionary();
var actionContext = new ActionContext(httpContext, new RouteData(), new PageActionDescriptor(), modelState);
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, modelState);
var tempData = new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>());
var pageContext = new PageContext(actionContext)
{
    ViewData = viewData
};
var pageModel = new MyPageModel()
{
    PageContext = pageContext,
    TempData = tempData,
    Url = new UrlHelper(actionContext),
    MetadataProvider = modelMetadataProvider
};

// Act    
var result = await pageModel.GetPartial();

// Assert
Assert.IsType<PartialViewResult>(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么单元测试在第一次运行时会失败? - Why does unit test fail on first run? 为什么此Microsoft单元测试失败? - Why does this Microsoft Unit Test Fail? Razor PageHandler PartialViewResult Model 类型不匹配 - Razor PageHandler PartialViewResult Model Type mismatch 为什么不指定视图名称会导致我的单元测试失败? - Why does not specifying view name cause my unit test to fail? 为什么在比较两个双打时这个单元测试失败了? - Why does this unit test fail when comparing two doubles? 为什么Task.ContinueWith无法在此单元测试中执行? - Why does the Task.ContinueWith fail to execute in this Unit Test? 为什么这个单元测试在我的机器上传递但在构建服务器上失败? - Why does this unit test pass on my machine but fail on the build server? 我如何单元测试返回PartialViewResult的MVC Action? - How can I unit test an MVC Action that returns a PartialViewResult? 为什么 SonarQube 声称单元测试未涵盖“返回”行? - Why does SonarQube claim the `return` line is not covered by a unit test? 为什么从单元测试中的控制器强制转换结果返回null? - Why does casting the result from a controller in a unit test return null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM