Setting Up Jason Parsing Tests Using Dynamic Objects Or Strings
[Test] public void SomeTestUsingExpandoObject() { // arrange const string getItemEventDataValue = @"{ ""_PrimaryId"": ""F:\\dasda\\ToMO-005775.json"", ""_SecondaryId"": ""123456"", ""RetrievalResult"": ""Processed Successfully"" }"; dynamic indexItemRaw = new ExpandoObject(); indexItemRaw.id = sourceIndexItemId; indexItemRaw.version = sourceIndexItemVersion.ToString(CultureInfo.InvariantCulture); dynamic source = new ExpandoObject(); source.getItemEventData = getItemEventDataValue; indexItemRaw.source = source; // act IEnumerable<ClaimDto> claimsEnum = someMapper.Map(JObject.FromObject(indexItemRaw)); var claimDtos = claimsEnum.ToList(); //assert ... } [Test] public void SomeTestUsingJSonStringOnly() { //How to set up unit tests using strings to set up the data, instead of dynamic objects. //Sometimes it can be simpler, especially if you retrieve the raw data from a run //Here is the above test, arranged using a string as an example //In this case I thought it was more complicated to set everything up as a string //(because one of the json string object entries (the "getItemEventData") was itself a json string) // arrange string rawJToken = @" { ""id"": ""{sourceIndexItemId}"", ""version"": ""1.0"", ""source"": { ""getItemEventData"": ""{ \""PrimaryId\"": \""F:\\\\dasda\\\\ToMO-005775.json\"", \""SecondaryId\"": \""123456\"" \""RetrievalResult\"": \""Processed Successfully\"" }"" } }"; rawJToken = rawJToken.Replace("{sourceIndexItemId}", sourceIndexItemId); JToken jtoken = JToken.Parse(rawJToken); //act IEnumerable<ClaimDto> claimsEnum = someMapper.Map(jtoken); var claimDtos = claimsEnum.ToList(); //assert ... }