如何验证 ActualJSon FluentAssertion 中是否存在 expectedJson

问题描述 投票:0回答:0

我是流利断言的新手,有问题,我有两个不同的 JSON 文件,一个是我的实际 Json,一个是现在预期的,当我使用断言方法时,为了检查我的预期 Json 是否存在于我的实际 Json 中,但是当我使用像这样的 Contain 、 HasElement 和 HasValue 方法时,它确实获得了价值, Should().Contain , Should().HasElement 或 Should().HasValue

我收到以下错误:- system.ArgumentException:访问的 JArray 值具有无效键值“7899”,预期为 Int32 数组索引。

我想检查我的预期 json 块是否存在于我的实际 json 中,以便我可以验证实际值是否包含或具有预期 json 的值 “

实际 Json :-

{
  "AId": "7899",
  "BuyerBId": "600032",

  "TestData": [
     {
      "tooID": "123456b",
       "testArray": ["12345888","01010101"]
     }
      ],
      "Catalogue": {
        "Name": "Elon",
        "Age": 22
},
"CountryPriceA": [
  {
    
   
    "ExportPrice": [
      {
        "NewPrice": 24,
        "OldPrice": 45
      },
      {
        "NewPrice": 100,
        "OldPrice": 2
      },
      {
        "NewPrice": 25,
        "OldPrice": 8
      }
    ]
  }
  ],
  "CountryPriceB": [
    {
      
     
      "ExportPrice": [
        {
          "NewPrice": 24,
          "OldPrice": 45
        },
        {
          "NewPrice": 100,
          "OldPrice": 2
        },
        {
          "NewPrice": 25,
          "OldPrice": 8
        }
      ]
    }
    ] ,
    "CountryPriceC": [
      {
        
       
        "ExportPrice": [
          {
            "NewPrice": 24,
            "OldPrice": 45
          },
          {
            "NewPrice": 100,
            "OldPrice": 2
          },
          {
            "NewPrice": 25,
            "OldPrice": 8
          }
        ]
      }
      ] ,
      "CountryPriceD": [
        {
          
         
          "ExportPrice": [
            {
              "NewPrice": 24,
              "OldPrice": 45
            },
            {
              "NewPrice": 100,
              "OldPrice": 2
            },
            {
              "NewPrice": 25,
              "OldPrice": 8
            }
          ]
        }
        ] 
        
}

预期的 Json:-

{
  "AId": "7899",
  "BuyerBId": "600032",

  "TestData": [
     {
      "tooID": "123456b",
       "testArray": ["12345888","01010101"]
     }
      ],
      "Catalogue": {
        "Name": "Elon",
        "Age": 22
}
"CountryPrice": [
  {
    
   
    "ExportPrice": [
      {
        "NewPrice": 24,
        "OldPrice": 45
      },
      {
        "NewPrice": 100,
        "OldPrice": 2
      },
      {
        "NewPrice": 25,
        "OldPrice": 8
      }
    ],
  }
  ] 
        
}

这是我的代码:-

```
var jsonObjectActual = JObject.Parse(jsonStringActual);
var jsonObjectExpected = JObject.Parse(jsonStringExpected);

            foreach (JProperty prop in jsonObjectExpected.Properties())
            {
                foreach (JProperty prop1 in jsonObjectActual.Properties())
                {
                  
                  var expectedPropertyValue = jsonObjectExpected.GetValue(prop.Name);
                  Console.WriteLine(prop.Name + " " + expectedPropertyValue);
                  var actualPropertyValue = jsonObjectActual.GetValue(prop1.Name);
                  Console.WriteLine(prop.Name + " " + actualPropertyValue);
                    actualPropertyValue.Should().HaveElement(expectedPropertyValue.ToString());

} } `´´

c# nunit fluent-assertions
© www.soinside.com 2019 - 2024. All rights reserved.