Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to validate if the APIs contains the same property and value of the model that its being compared to. I have a Validate method which where I am deserializing the response from the server. I also have the DTO which the model. Then using Refection I am getting the property and values. But in Assert line its failing because the API server returns a Array and in that Arrray contains the model I am trying to compare it with. I know I can use Assert.equal because server can return other information as well.. How can I use contains or some other way to verify this.

public class PropertiesValidator<TModel> 
    {
        public static void Validate(IRestResponse response, TModel modelObj, params string[] propertiesNotToCompare)
        {
            var content = response.Content;
            object acutalObjects = JsonConvert.DeserializeObject(content);

            PropertyInfo[] properties = acutalObjects.GetType().GetProperties();
            foreach (PropertyInfo currentRealProperty in properties)
            {
                if (!propertiesNotToCompare.Contains(currentRealProperty.Name))
                {
                    PropertyInfo currentExpectedProperty = modelObj.GetType().GetProperty(currentRealProperty.Name);
                    string exceptionMessage =
                        string.Format("The property {0} of class {1} was not as expected.", currentRealProperty.Name, currentRealProperty.DeclaringType.Name);
                    Assert.AreEqual(currentExpectedProperty.GetValue(modelObj, null), currentRealProperty.GetValue(acutalObjects, null), exceptionMessage);
       
                }
                   
            }


        }
           
    }

//Here is my Model DTO class

  public class TagDTO
    {
        public long id { get; set; }
        public string name{ get; set; }
        public long type{ get; set; }
        public object query{ get; set; }
        public object parentId { get; set; }
        public object ownerId { get; set; }
        public object activeDirectoryId { get; set; }
        public bool hasChildren { get; set; }

        public static TagDTO actualObject()
        {
            return new TagDTO()
            {
                id = 5,
                name = "NotTag",
                type = 0,
                query = null,
                parentId = null,
                ownerId = null,
                activeDirectoryId = null,
                hasChildren = false
            };

        }

    }
//Here is the Json response from the Server.

[
  {
    "id": 5,
    "name": "NoTag",
    "type": 0,
    "query": null,
    "parentId": null,
    "ownerId": null,
    "activeDirectoryId": null,
    "hasChildren": false
  },
  {
    "id": 6,
    "name": "MyTag",
    "type": 0,
    "query": null,
    "parentId": null,
    "ownerId": "eccf46f3-348b-422e-8789-c163b5953b41",
    "activeDirectoryId": null,
    "hasChildren": false
  }
]


What I have tried:

Not sure.. maybe we can use CollectionAssert.Contains()
Posted
Updated 26-Jun-20 11:47am
v2

1 solution

attemping to deserialize Jason to a model object will throw an exception if thedesrialization can’t be performed. Why can’t you just put a try/catch block around the deserialization code and let nature take its course?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900