mercredi 19 septembre 2018

C# Unit testing a Newtonsoft JSON model

I am starting with unit testing and am doing so with XUnit.

I have a model :

using Newtonsoft.Json;
namespace HomeAddressSearch
{
    public class Properties
    {
        [Required]
        [JsonProperty(PropertyName = "civic_number")]
        public string CivicNumber { get; set; }

        [JsonProperty(PropertyName = "address")]
        public string Address { get; set; }

        [JsonProperty(PropertyName = "postal_code")]
        public string PostalCode { get; set; }

        [JsonProperty(PropertyName = "city_name")]
        public string CityName { get; set; }
    }
}

and I have a JSON from which I am getting values like :

[
  {
    "properties": {
      "civic_number": "100",
      "address": "100, king street",
      "postal_code": "H0H0H0",
      "city_name": "Windsor"
    },
    "owner": {
      "name": "John Doe",
      "age": "40"
    },
  }
]

So I want to test that model and for so I though of:

  • As CivicNumber is Required I want to make sure there is no Null value or empty string in the JSON, so a test with a null value crashing the test
  • If ever someone would change the JSON and remove one of the field in the properties object, like city_name, then the test would fail so I know I either need to adapt my model or get back to the original JSON format
  • Check that format of JSON values, in this case all of them are string, are ok to be injected in my model
  • Run an injection from a shorter version of a JSON file with like only 3 entries into the model to make sure everything is ok

Am I on the right path and how to do this, that would greatly help and kickstart me for a lot of other things.

THanks!

Aucun commentaire:

Enregistrer un commentaire