C#单元测试-JSON断言

时间:2018-11-23 17:03:40

标签: c# unit-testing nunit assertion fluent-assertions

我只是在玩一些Json和Fluentassertions,我能够成功调用API,获取结果,反序列化它们,但是由于某种原因,当我对响应做出断言时,它丢失了数据它是空的。我已经调试,可以看到数据流过,然后在断言期间丢失。

任何帮助表示赞赏。

{
    [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"
  }
]}

2 个答案:

答案 0 :(得分:3)

基于来自moviedb的预期JSON使用以下强类型定义

/**
 * <p>Computes the coordinates of this view on the screen. The argument
 * must be an array of two integers. After the method returns, the array
 * contains the x and y location in that order.</p>
 *
 * @param outLocation an array of two integers in which to hold the coordinates
 */
public void getLocationOnScreen(@Size(2) int[] outLocation) {
    getLocationInWindow(outLocation);

    final AttachInfo info = mAttachInfo;
    if (info != null) {
        outLocation[0] += info.mWindowLeft;
        outLocation[1] += info.mWindowTop;
    }
}

您需要在测试中执行以下操作

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; }
}

答案 1 :(得分:0)

Here是JSON的fluentassertions扩展,其中包含许多断言JSON的有用方法:

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

我不是该图书馆的作者。