How to unit test an Action method which returns JsonResult?

前端 未结 8 1956
我在风中等你
我在风中等你 2020-12-13 12:38

If I have a controller like this:

[HttpPost]
public JsonResult FindStuff(string query) 
{
   var results = _repo.GetStuff(query);
   var jsonResult = results         


        
8条回答
  •  臣服心动
    2020-12-13 13:00

    My solution is to write the extension method:

    using System.Reflection;
    using System.Web.Mvc;
    
    namespace Tests.Extensions
    {
        public static class JsonExtensions
        {
            public static object GetPropertyValue(this JsonResult json, string propertyName)
            {
                return json.Data.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public).GetValue(json.Data, null);
            }
        }
    }
    

提交回复
热议问题