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);
}
}
}
}
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
};
}
}
[
{
"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()