[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>
/// Gets the <see cref="ModelStateDictionary"/> that contains the state of the model and of model-binding validation.
/// </summary>
public ModelStateDictionary ModelState => ControllerContext?.ModelState;
public ModelStateDictionary ModelState => ControllerContext.ModelState;
/// <summary>
/// Gets or sets the <see cref="Mvc.ControllerContext"/>.

View File

@ -30,17 +30,38 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
private IObjectModelValidator _objectValidator;
private ITempDataDictionary _tempData;
private IUrlHelper _urlHelper;
private PageContext _pageContext;
/// <summary>
/// Gets the <see cref="RazorPages.PageContext"/>.
/// </summary>
[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>
/// Gets the <see cref="Http.HttpContext"/>.
/// </summary>
public HttpContext HttpContext => PageContext?.HttpContext;
public HttpContext HttpContext => PageContext.HttpContext;
/// <summary>
/// Gets the <see cref="HttpRequest"/>.

View File

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