如何在ASP.NET MVC3网站上对我的Json结果进行单元测试?

时间:2021-09-23 06:52:23

I'm trying to test the Data values that are returned from an ASP.NET MVC3 JsonView, but I'm not sure how.

我正在尝试测试从ASP.NET MVC3 JsonView返回的Data值,但我不确定如何。


I have a simple ASP.NET MVC3 website, with an action method that returns a JsonView.

我有一个简单的ASP.NET MVC3网站,有一个返回JsonView的动作方法。

eg (some pseduo code for a list of anonymous types):

例如(一些匿名类型列表的pseduo代码):

var lotsOfFail = database.GetMeThatDamnDataList();
var returnData = (from x in lotsOfFail
                  select new
                  {
                      Id = x.Id,
                      Name = x.Name
                      ..
                   }).ToList();
return Json(returnData, JsonRequestBehavior.AllowGet);

Now in my unit test, I'm trying to test the values of Data. So following various suggestions, I'm doing the following.. which -does- work :-

现在在我的单元测试中,我正在尝试测试数据的值。所以根据各种建议,我正在做以下事情......哪些 - 工作: -

// Act.
JsonResult jsonResult = controller.PewPewKThxBai(null, null);

// Assert.    
Assert.IsNotNull(jsonResult);
dynamic data = jsonResult.Data;
Assert.IsNotNull(data);
Assert.IsTrue(data.Count >= 0);

But I also wish to test the first three results that come back, against a fixed list of data.

但我也希望测试前三个结果,对照固定的数据列表。

Notice how I have the following code: var lotsOfFail = database.GetMeThatDamnDataList(); Well, the database is populated with some hardcoded data AND some random data. The first three records are hardcoded.

请注意我如何使用以下代码:var lotsOfFail = database.GetMeThatDamnDataList();好吧,数据库中填充了一些硬编码数据和一些随机数据。前三个记录是硬编码的。

As such, I wish to make sure that I can test my hardcoded data.

因此,我希望确保我可以测试我的硬编码数据。

Like this...

喜欢这个...

// Assert.    
Assert.IsNotNull(jsonResult);
dynamic data = jsonResult.Data;
Assert.IsNotNull(data);

var hardCodedData =
    FakeWhatevers.CreateHardcodedWhatevers()
    .Where(x => x.EventType == EventType.BannableViolation)
    .ToList();
Assert.IsTrue(data.Count >= hardCodedData .Count);

for (int i = 0; i < hardCodedData .Count; i++)
{
    Assert.AreEqual(data[0].Id== hardCodedData [0].GameServerId);
}

but because data is a dynamic, I don't know how to test the properties of it.

但由于数据是动态的,我不知道如何测试它的属性。

Any ideas?

有任何想法吗?

1 个解决方案

#1


9  

The following should work:

以下应该有效:

for (int i = 0; i < hardCodedData.Count; i++)
{
    Assert.AreEqual(hardCodedData[i].GameServerId, data[i].Id);
    Assert.AreEqual(hardCodedData[i].GameServerName, data[i].Name);
    ...
}

Notice that I have inverted the order of argument as the first is the expected and the second is the actual.

请注意,我已经颠倒了参数的顺序,因为第一个是预期的,第二个是实际的。

#1


9  

The following should work:

以下应该有效:

for (int i = 0; i < hardCodedData.Count; i++)
{
    Assert.AreEqual(hardCodedData[i].GameServerId, data[i].Id);
    Assert.AreEqual(hardCodedData[i].GameServerName, data[i].Name);
    ...
}

Notice that I have inverted the order of argument as the first is the expected and the second is the actual.

请注意,我已经颠倒了参数的顺序,因为第一个是预期的,第二个是实际的。