3

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"
  }
]}
Miko
  • 109
  • 1
  • 7
  • the JSON shown has no `cast` key. Is the JSON shown accurate? – Nkosi Nov 23 '18 at 17:05
  • When i make the call to the API the cast key is there `{"cast":` But JArray removes it i believe and just stores the above – Miko Nov 23 '18 at 17:19
  • that does not sound accurate. The shown JSON does not match the `Actors` object model definition – Nkosi Nov 23 '18 at 17:22
  • This is the JSON when i check in Postman: {[ `{"cast":[{ "character": "Daenerys Targaryen", "credit_id": "5256c8af19c2956ff60479f6", "gender": 1, "id": 1223786, "name": "Emilia Clarke", "order": 0, "profile_path": "/lRSqMNNhPL4Ib1hAJxmDFBXHAMU.jpg" } ]}` When i debug and inspect `cast` in the model definition it is storing it without the `{"cast":` – Miko Nov 23 '18 at 17:30
  • I called the API in the browser and got `{"cast":[{"character":"Daenerys Targaryen",....` – Nkosi Nov 23 '18 at 17:44
  • I used the JSON to generate the classes in my answer and was able to assert the response data as shown below. – Nkosi Nov 23 '18 at 17:45
  • That works great! Im guessing the approach below is only applicable if there are multiple parent nodes each with child nodes? – Miko Nov 23 '18 at 18:31

2 Answers2

3

Here is fluentassertions extension for JSON, which contains many useful methods for asserting JSON:

Available extension methods BeEquivalentTo() ContainSingleElement() ContainSubtree() HaveCount() HaveElement() HaveValue() MatchRegex() NotBeEquivalentTo() NotHaveElement() NotHaveValue() NotMatchRegex()

I am not an author of this library.

Karel Kral
  • 5,297
  • 6
  • 40
  • 50
2

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");
Nkosi
  • 235,767
  • 35
  • 427
  • 472