如何对返回匿名对象的 ASP.NET Core MVC 控制器进行单元测试?

编程入门 行业动态 更新时间:2024-10-25 18:25:39
本文介绍了如何对返回匿名对象的 ASP.NET Core MVC 控制器进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在对返回匿名对象的 ASP.NET Core MVC 控制器进行单元测试时遇到问题.单元测试设置在单独的项目中,直接从主项目调用控制器方法.

I'm having trouble unit testing ASP.NET Core MVC controllers that return anonymous objects. The unit testing is set up in a separate project and calls the controller methods from the main project directly.

控制器方法返回 IActionResult 但通常这些是 OkObjectResult 和 BadRequestObjectResult 对象,它们被转换为具有适当 HTTP 状态代码的 JSON 响应.匿名对象作为 ObjectResult 对象的构造函数参数传递,我正试图针对这些对象进行断言(可通过 ObjectResult.Value 访问).

The controller methods return IActionResult but typically these are OkObjectResult and BadRequestObjectResult objects that get translated into a JSON response with the appropriate HTTP status code. The anonymous objects are passed as the constructor parameters for the ObjectResult objects and it is these I'm trying to make assertions against (accessible via ObjectResult.Value).

我发现了这个问题(如何在asp 5) 的答案是使用动态和添加

I found this question (how can i access internals in asp 5) that has an answer that says to use dynamics and add

[assembly: InternalsVisibleTo("Namespace")]

到 AssemblyInfo.cs 以允许测试项目访问匿名对象的内部对象属性.但是,最新版本的 ASP.NET Core MVC 没有 AssemblyInfo.cs,并且按照链接问题的答案中的建议添加一个也不起作用.

to AssemblyInfo.cs to allow the test project access to the internal object properties of the anonymous objects. However, latest versions of ASP.NET Core MVC do not have AssemblyInfo.cs and adding one as suggested in the answers to the linked question does not work either.

现在有其他位置可以添加 InternalsVisibleTo 还是我遗漏了什么?

Is there now a different location to add the InternalsVisibleTo or am I missing something?

推荐答案

来自 this answer 的原始想法,更通用方法.使用自定义 DynamicObject 作为通过反射检查值的包装器,无需添加 InternalsVisibleTo

Original idea from this answer with a more generic approach. Using a custom DynamicObject as a wrapper for inspecting the value via reflection there was no need to add the InternalsVisibleTo

public class DynamicObjectResultValue : DynamicObject, IEquatable<DynamicObjectResultValue> { private readonly object value; public DynamicObjectResultValue(object value) { this.value = value; } #region Operators public static bool operator ==(DynamicObjectResultValue a, DynamicObjectResultValue b) { // If both are null, or both are same instance, return true. if (System.Object.ReferenceEquals(a, b)) { return true; } // If one is null, but not both, return false. if (ReferenceEquals((object)a, null) || ReferenceEquals((object)b, null)) { return false; } // Return true if the fields match: return a.value == b.value; } public static bool operator !=(DynamicObjectResultValue a, DynamicObjectResultValue b) { return !(a == b); } #endregion public override IEnumerable<string> GetDynamicMemberNames() { return value.GetType().GetProperties().Select(p => p.Name); } public override bool TryGetMember(GetMemberBinder binder, out object result) { //initialize value result = null; //Search possible matches and get its value var property = value.GetType().GetProperty(binder.Name); if (property != null) { // If the property is found, // set the value parameter and return true. var propertyValue = property.GetValue(value, null); result = propertyValue; return true; } // Otherwise, return false. return false; } public override bool Equals(object obj) { if (obj is DynamicObjectResultValue) return Equals(obj as DynamicObjectResultValue); // If parameter is null return false. if (ReferenceEquals(obj, null)) return false; // Return true if the fields match: return this.value == obj; } public bool Equals(DynamicObjectResultValue other) { // If parameter is null return false. if (ReferenceEquals(other, null)) return false; // Return true if the fields match: return this.value == other.value; } public override int GetHashCode() { return ToString().GetHashCode(); } public override string ToString() { return string.Format("{0}", value); } }

假设有以下控制器

public class FooController : Controller { public IActionResult GetAnonymousObject() { var jsonResult = new { id = 1, name = "Foo", type = "Bar" }; return Ok(jsonResult); } public IActionResult GetAnonymousCollection() { var jsonResult = Enumerable.Range(1, 20).Select(x => new { id = x, name = "Foo" + x, type = "Bar" + x }).ToList(); return Ok(jsonResult); } }

测试可能看起来像

[TestMethod] public void TestDynamicResults() { //Arrange var controller = new FooController(); //Act var result = controller.GetAnonymousObject() as OkObjectResult; //Assert dynamic obj = new DynamicObjectResultValue(result.Value); Assert.IsNotNull(obj); Assert.AreEqual(1, obj.id); Assert.AreEqual("Foo", obj.name); Assert.AreEqual(3, obj.name.Length); Assert.AreEqual("Bar", obj.type); } [TestMethod] public void TestDynamicCollection() { //Arrange var controller = new FooController(); //Act var result = controller.GetAnonymousCollection() as OkObjectResult; //Assert Assert.IsNotNull(result, "No ActionResult returned from action method."); dynamic jsonCollection = result.Value; foreach (dynamic value in jsonCollection) { dynamic json = new DynamicObjectResultValue(value); Assert.IsNotNull(json.id, "JSON record does not contain "id" required property."); Assert.IsNotNull(json.name, "JSON record does not contain "name" required property."); Assert.IsNotNull(json.type, "JSON record does not contain "type" required property."); } }

更多推荐

如何对返回匿名对象的 ASP.NET Core MVC 控制器进行单元测试?

本文发布于:2023-11-14 00:58:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   单元测试   对象   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!