Created
May 15, 2019 22:00
-
-
Save BryanWilhite/73f446d8de646b74b74147199577d925 to your computer and use it in GitHub Desktop.
xUnit: A test object for IActionResult types, wrapping JsonResult, NoContentResult, ObjectResult etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Mvc; | |
using Xunit; | |
namespace My.Tests.Models | |
{ | |
/// <summary> | |
/// Defines all subclasses of <see cref="ActionResult"/> | |
/// that have status codes and <see cref="object"/> values. | |
/// </summary> | |
public class ObjectValueActionResult | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ObjectValueActionResult"/> class. | |
/// </summary> | |
/// <param name="result">The result.</param> | |
/// <remarks> | |
/// The majority of <see cref="IActionResult"/> instances like <see cref="OkObjectResult"/> derive | |
/// from <see cref="ObjectResult"/>. | |
/// </remarks> | |
public ObjectValueActionResult(IActionResult result) | |
{ | |
Assert.NotNull(result); | |
switch (result) | |
{ | |
case JsonResult j: | |
this.Value = j.Value; | |
this.StatusCode = j.StatusCode; | |
break; | |
case NoContentResult n: | |
this.StatusCode = n.StatusCode; | |
break; | |
case ObjectResult o: | |
this.Value = o.Value; | |
this.StatusCode = o.StatusCode; | |
break; | |
default: | |
throw new NotImplementedException($"The expected {nameof(IAsyncResult)} type is not here."); | |
} | |
} | |
/// <summary> | |
/// Gets or sets the value. | |
/// </summary> | |
/// <value> | |
/// The value. | |
/// </value> | |
public object Value { get; set; } | |
/// <summary>Gets or sets the HTTP status code.</summary> | |
public int? StatusCode { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JsonResult
andObjectResult
do not derive from each other and controller methods are often designed to return either type usingIActionResult
orActionResult
; i baffles me whyJsonResult
andObjectResult
are so unrelated—surely someone at Microsoft (like @DamianEdwards) can explain