如何进行单元测试,它返回JsonResult的操作方法? [英] How to unit test an Action method which returns JsonResult?

查看:212
本文介绍了如何进行单元测试,它返回JsonResult的操作方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个这样的控制器:

If I have a controller like this:

[HttpPost]
public JsonResult FindStuff(string query) 
{
   var results = _repo.GetStuff(query);
   var jsonResult = results.Select(x => new
   {
      id = x.Id,
      name = x.Foo,
      type = x.Bar
   }).ToList();

   return Json(jsonResult);
}

基本上,我抢东西从我的资料库,然后将其投射到一个列表< T> 匿名类型

我怎么能单元测试?

System.Web.Mvc.JsonResult 有一个名为数据,但它是类型<$ C $财产C>对象,如我们预期。

System.Web.Mvc.JsonResult has a property called Data, but it's of type object, as we expected.

这是否意味着,如果我想测试JSON对象有我期望的属性(ID,姓名,类型),我不得不使用反射?

So does that mean if I want to test that the JSON object has the properties I expect ("id", "name", "type"), I have to use reflection?

编辑:

下面是我的测试:

// Arrange.
const string autoCompleteQuery = "soho";

// Act.
var actionResult = _controller.FindLocations(autoCompleteQuery);

// Assert.
Assert.IsNotNull(actionResult, "No ActionResult returned from action method.");
dynamic jsonCollection = actionResult.Data;
foreach (dynamic json in jsonCollection)
{
   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.");
}

但我得到一个运行时错误在回路中,指出对象不包含一个定义ID。

But I get a runtime error in the loop, stating "object does not contain a definition for id".

在我的断点, actionResult.Data 定义为列表&LT; T&GT; 匿名类型,所以我只要我通过这些列举,我可以检查的属性。在循环内部,物体的确实的所谓身份证的属性 - 所以不知道问题是什么

When I breakpoint, actionResult.Data is defined as a List<T> of anonymous types, so I figure if I enumerate through these, I can check the properties. Inside the loop, the object does have a property called "id" - so not sure what the issue is.

推荐答案

RPM,你看是正确的。我还有很多需要学习动态,我不能让马克的方式来工作的。因此,这里是我以前的样子做。您可能会发现它有用。我只是写了一个简单的扩展方法:

RPM, you look to be correct. I still have much to learn about dynamic and I cannot get Marc's approach to work either. So here is how I was doing it before. You may find it helpful. I just wrote a simple extension method:

    public static object GetReflectedProperty(this object obj, string propertyName)
    {  
        obj.ThrowIfNull("obj");
        propertyName.ThrowIfNull("propertyName");

        PropertyInfo property = obj.GetType().GetProperty(propertyName);

        if (property == null)
        {
            return null;
        }

        return property.GetValue(obj, null);
    }

然后,我只是用它来做我的JSON数据断言:

Then I just use that to do assertions on my Json data:

        JsonResult result = controller.MyAction(...);
                    ...
        Assert.That(result.Data, Is.Not.Null, "There should be some data for the JsonResult");
        Assert.That(result.Data.GetReflectedProperty("page"), Is.EqualTo(page));

这篇关于如何进行单元测试,它返回JsonResult的操作方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆