Skip to content

StepDefention: Response Content

Mahmoud A. Zaid edited this page Mar 15, 2019 · 3 revisions

Now in this lesson, we will implement the step definition to deserialize the response by creating a model for each API

1. Right-CLick on the project > Add new folder > name Model

2. Under the model folder > create Employee property class

you can find this class here

The API result showing as below:

{"id":"719","employee_name":"test","employee_salary":"123","employee_age":"23","profile_image":""}

So in the Employee class we will define API response proprites

public string id { get; set; }

public string employee_name { get; set; }

public string employee_salary { get; set; }

public string employee_age { get; set; }

public string profile_image { get; set; }

3. Under StepDefntion Folder > Create a new StepDention Class

4. Implemet deserialization step to deserialize the response

  • Define an object from Employee class private static Employee employee_info;
  • define JsonDeserializer private JsonDeserializer deserializer = new JsonDeserializer(); and now the implementation of StepDefination showing as below
[When(@"Deserialize the employee api content")]
public void WhenDeserializeTheEmployeeApiContent()
{
employee_info = deserializer.Deserialize<Employee>(SharedSteps.apiResult);
var type = employee_info.GetType();
}

5. Implement the Assertion step

  • Firstly convert the returned table from feature file to an instance from the Employee class var employee = table.CreateInstance<Employee>();
  • Now Assert by FluentAssertion employee_info.Should().BeEquivalentTo(employee); so the final implementation of the StepDefintion should be like that:
[Then(@"The employee should have the following values")]
public void ThenTheEmployeeShouldHaveTheFollowingValues(Table table)
{
var employee = table.CreateInstance<Employee>();
employee_info.Should().BeEquivalentTo(employee);
}

You can find the final class implementation of this lesson here

Clone this wiki locally