Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
¿Cómo obtener valores de JsonArray deserializado en c #?

Tengo una clase modelo en C# como se muestra a continuación:

 public class Persons { public String Name {get; set;} public int Age {get; set;} public String InsuranceId {get; set;} }

Estoy deserializando una parte de la respuesta de la API en esta clase.

 { "persons": [ { "name": "SteveBruns", "age": 24, "insuranceId": "M2409891" }, { "name": "JohnStash", "age": 34, "insuranceId": "N2789012" } ] }

Tengo que validar todos los campos name y insuranceId en mi proyecto SpecFlow. Digamos que tenemos un archivo de características de la siguiente manera:

 Feature: To hit a GET endpoint and fetch the person details @Feature-106 Scenario: Fetch the details and validate insurance Id Given I have a valid token When I create a GET request for /api/insuranceDetails Then I should validate <name1>, <name2>, <insuranceId1> and <insuranceId2> fields Examples: |name1 |insuranceId1 |name2 |insuranceId2| |SteveBruns |M2409891 |JohnStash |N2789012 |

Estas son las definiciones de los pasos.

 [Given(@"I have a valid token")] public void method() {} [When(@"I have a valid token")] public void method() {} [Then(@"I have a valid token")] public void method(String name1, String insureanceId1, String name2, String insureanceId2) { // some code here var details = obj.Persons; // obj is an object of some other class where Person is residing in it // I have tried the following // I want to extend this for name2 and insuranceId2 // In a nut shell, in the same testcase, I have to validate both names and insuranceId keys inside Persons JSONArray! foreach (item in details) { item.Name.Should().Be(name1) item.InsuranceId.Should().Be(insuranceId1) } // PS: tried this approach instead of using above foreach List<String> names = new List<String>(); names.add(name1); names.add(name2); List<String> insuranceIds = new List<String>(); insuranceIds.add(insuranceId1); insuranceIds.add(insuranceId2); for (int i = 0; i < details.size(); i++) { String expectedName = details[i].Name; String expectedInsruanceId = details[i].InsuranceId; String actualName = names[i]; String actualInsuranceId = insuranceIds[i]; expectedName.Should().Be(actualName); expectedInsruanceId.Should().Be(actualInsuranceId ); } }

¿Cuál es la mejor manera de lograr esto? ¿Usando Lambdas (seleccionar)? Cualquier otra sugerencia, por favor ayúdame aquí!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usar una tabla de datos horizontal en su paso Then y utilizar los ayudantes de la tabla debería ser el truco. Además, no veo un uso para una tabla de ejemplos (a menos que este sea un ejemplo abreviado del escenario).

 Feature: To hit a GET endpoint and fetch the person details @Feature-106 Scenario: Fetch the details and validate insurance Id Given I have a valid token When I create a GET request for /api/insuranceDetails Then the insurance details should be: | Name | Insurance Id | | SteveBruns | M2409891 | | JohnStash | N2789012 |

Y la definición del paso es bastante sencilla, suponiendo que los encabezados de columna de la tabla de datos de SpecFlow coincidan con los nombres de propiedad de cada objeto Person en el resultado JSON:

 [@Then(@"Then the insurance details should be:")] public void ThenTheInsuranceDetailsShouldBe(Table table) { var details = obj.Persons; // obj is an object of some other class where Person is residing in it // if the order of the collection does not matter table.CompareToSet(details); // if order matters table.CompareToSet(details, true); }

Dado que su cliente quiere usar el desarrollo basado en el comportamiento, considere reformular sus pasos para hablar su idioma comercial:

 @Feature-106 Scenario: Fetch the details and validate insurance Id Given I have a valid token When I request the insurance details Then the insurance details should be: | Name | Insurance Id | | SteveBruns | M2409891 | | JohnStash | N2789012 |

El punto final y el hecho de que es una solicitud GET es un detalle de implementación que no necesita comunicarse en un escenario BDD. Concéntrese primero en el proceso comercial.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!