I have the following code in a Unit Test:
SomeModel test = new SomeModel();
test.TestField = "Some Content Here"; //Where TestField is a String attribute of the SomeModel class
var expected = Json(test, JsonRequestBehavior.AllowGet);
When I run
System.Diagnostics.Debug.WriteLine("Expected " + expected.Data.ToString());
I get this in the output: Expected Fake.Path.SomeModel
When I run the test through the debugger I am able to see the data: Debugger Result
How can I access the data through my unit test? I want to be able to verify that the data in it is correct through automated tests.
I am using Visual Studio 2012 Professional
Thanks!
2 Answers 2
The default implementation (in Object
) of ToString
is to display the type name. That's what you are seeing when you do this:
expected.Data.ToString()
If you want to get at the actual TestField
, and Data
is defined as object, then you need:
((SomeModel)expected.Data).TestField
Comments
I would download Newtonsoft's JSON library from NuGet if you haven't already. This is the code I used on one of my applications which used that libraries JObject
class:
var parsed = JObject.Parse(jsonString);
JToken token;
if (parsed.TryGetValue("Key", out token))
{
var value = token.ToString();
... //Do stuff
}
3 Comments
myJsonResult.ToString()
or maybe myJsonResult.Data.ToString()
whichever returns the JSON formatted string containing the data.
ToString
is just to show the type name, which is what you are seeing. If you want the message, it's going to beexpected.Data.TestField
((SomeModel)expected.Data).TestField)