[Fixes #6943] Unit Testing Page Model Throws Null Ref On ModelState check

This commit is contained in:
Javier Calvarro Nelson 2017-10-26 14:37:42 -07:00 committed by GitHub
parent c567a690bc
commit 0989e60f73
3 changed files with 34 additions and 3 deletions

View File

@ -56,7 +56,7 @@ namespace Microsoft.AspNetCore.Mvc
/// <summary> /// <summary>
/// Gets the <see cref="ModelStateDictionary"/> that contains the state of the model and of model-binding validation. /// Gets the <see cref="ModelStateDictionary"/> that contains the state of the model and of model-binding validation.
/// </summary> /// </summary>
public ModelStateDictionary ModelState => ControllerContext?.ModelState; public ModelStateDictionary ModelState => ControllerContext.ModelState;
/// <summary> /// <summary>
/// Gets or sets the <see cref="Mvc.ControllerContext"/>. /// Gets or sets the <see cref="Mvc.ControllerContext"/>.

View File

@ -30,17 +30,38 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
private IObjectModelValidator _objectValidator; private IObjectModelValidator _objectValidator;
private ITempDataDictionary _tempData; private ITempDataDictionary _tempData;
private IUrlHelper _urlHelper; private IUrlHelper _urlHelper;
private PageContext _pageContext;
/// <summary> /// <summary>
/// Gets the <see cref="RazorPages.PageContext"/>. /// Gets the <see cref="RazorPages.PageContext"/>.
/// </summary> /// </summary>
[PageContext] [PageContext]
public PageContext PageContext { get; set; } public PageContext PageContext
{
get
{
if (_pageContext == null)
{
_pageContext = new PageContext();
}
return _pageContext;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_pageContext = value;
}
}
/// <summary> /// <summary>
/// Gets the <see cref="Http.HttpContext"/>. /// Gets the <see cref="Http.HttpContext"/>.
/// </summary> /// </summary>
public HttpContext HttpContext => PageContext?.HttpContext; public HttpContext HttpContext => PageContext.HttpContext;
/// <summary> /// <summary>
/// Gets the <see cref="HttpRequest"/>. /// Gets the <see cref="HttpRequest"/>.

View File

@ -24,6 +24,16 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
{ {
public class PageModelTest public class PageModelTest
{ {
[Fact]
public void PageContext_GetsInitialized()
{
// Arrange
var pageModel = new TestPageModel();
// Act & Assert
Assert.NotNull(pageModel.PageContext);
}
[Fact] [Fact]
public void Redirect_WithParameterUrl_SetsRedirectResultSameUrl() public void Redirect_WithParameterUrl_SetsRedirectResultSameUrl()
{ {