Search code examples
c#unit-testingnunitassertionfluent-assertions

C# Unit Testing - Assertions on JSON


I am just playing around with some Json and Fluentassertions, I am able to make a call to an API successfully, get the results, deserialize them but for some reason when i get to do an assertion on the response its losing the data and it empty. I have debugged, can see the data flowing through and then losing it during assertion.

Any help appreciated.

{
    [TestClass]
    public class UnitTest1
    {
        HttpClient client = new HttpClient();

         [TestMethod]
        public void ActorNotInSeason6Episode1()
        {
            try
            {
                //test = extent.CreateTest("Test 1");
                HttpResponseMessage respone = client.GetAsync("https://api.themoviedb.org/3/tv/1399/season/6/episode/1/credits?api_key=").Result;
                Assert.IsTrue(respone.IsSuccessStatusCode.Equals(true));
                string ResponseMessage = respone.Content.ReadAsStringAsync().Result;
                Actors actors = JsonConvert.DeserializeObject<Actors>(ResponseMessage);
                //var a = Actors.cast["cast"];
                //var names = a.Children[];
                //var a = actors.cast.Children();


           actors.cast.Should().Contain("Emilia Clarke", "Test");

            }
            catch(AssertFailedException)
            {
                Assert.Fail();

            }
        }

    }
}



    class Actors
    {
        public JArray cast  { get; set; }
        public JArray guest_stars { get; set; }

    }
}

JSON

{[
  {
    "character": "Daenerys Targaryen",
    "credit_id": "5256c8af19c2956ff60479f6",
    "gender": 1,
    "id": 1223786,
    "name": "Emilia Clarke",
    "order": 0,
    "profile_path": "/lRSqMNNhPL4Ib1hAJxmDFBXHAMU.jpg"
  }
]}

Solution

  • Using the following strongly typed definitions based on the expected JSON from themoviedb

    public partial class RootObject {
        [JsonProperty("cast")]
        public Cast[] Cast { get; set; }
    
        [JsonProperty("crew")]
        public Crew[] Crew { get; set; }
    
        [JsonProperty("guest_stars")]
        public Cast[] GuestStars { get; set; }
    
        [JsonProperty("id")]
        public long Id { get; set; }
    }
    
    public partial class Cast {
        [JsonProperty("character")]
        public string Character { get; set; }
    
        [JsonProperty("credit_id")]
        public string CreditId { get; set; }
    
        [JsonProperty("gender")]
        public long Gender { get; set; }
    
        [JsonProperty("id")]
        public long Id { get; set; }
    
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("order")]
        public long Order { get; set; }
    
        [JsonProperty("profile_path")]
        public string ProfilePath { get; set; }
    }
    
    public partial class Crew {
        [JsonProperty("id")]
        public long Id { get; set; }
    
        [JsonProperty("credit_id")]
        public string CreditId { get; set; }
    
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("department")]
        public string Department { get; set; }
    
        [JsonProperty("job")]
        public string Job { get; set; }
    
        [JsonProperty("gender")]
        public long Gender { get; set; }
    
        [JsonProperty("profile_path")]
        public string ProfilePath { get; set; }
    }
    

    You would need to do the following in your test

    //...
    
    var actors = JsonConvert.DeserializeObject<RootObject>(ResponseMessage);
    
    //Assert
    actors.Cast.Should().Contain(actor => actor.Name == "Emilia Clarke");