10

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


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

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

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.

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 Answer 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.

Sign up to request clarification or add additional context in comments.

3 Comments

is this because the dynamic's need to be second or something? why would ordering of the equation effect things?
Ah bleh :) I see my error now, also. I shouldn't have done an == but two arguments. Cheers :)
@Pure.Krome, ordering doesn't effect anything. It's just the convention. So that when your unit test fails you get a meaningful message: expected: 1, actual: 2. If you put the arguments in the wrong order when your test fails you will be wondering why on Earth do you expect 1, when my hardCodedData contains 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.