I'm looking for some general advice with an API framework I am setting up using C# and RestSharp as the client library. I've successfully set up an initial test, but I'm now looking at creating a series of assertions to ensure that certain values are returned in the response

My test looks as below:

        [Test]
    public void ShouldHaveDataAttributes()
    {
        var restClient = new RestClient("http://ergast.com/api/f1");

        var restRequest = new RestRequest("2016/circuits.json", Method.GET);

        var restResponse = restClient.Execute(restRequest);

        dynamic jsonResponse = JsonConvert.DeserializeObject(restResponse.Content);

        dynamic jsonObject = jsonResponse.MRData.CircuitTable;
        int circuitId = (int)jsonObject.season;
        System.Console.WriteLine(jsonObject);
    }

The jsonObject returns the below data:

{
  "season": "2016",
  "Circuits": [
    {
      "circuitId": "albert_park",
      "url": "http://en.wikipedia.org/wiki/Melbourne_Grand_Prix_Circuit",
      "circuitName": "Albert Park Grand Prix Circuit",
      "Location": {
        "lat": "-37.8497",
        "long": "144.968",
        "locality": "Melbourne",
        "country": "Australia"
      }
    }]
}

My question is how would I specifically check that, for example the correct "circuitId" is returned or that the "circuitName" value is "Albert Park"? I'm keen to carry on utilising RestSharp for my framework so was hoping there was an extension to allow me to do that

  • This could be a dup or extension of of stackoverflow.com/questions/25052293/…. – Kshetra Mohan Prusty Sep 7 at 8:44
  • Thanks for the link but this isn't a duplicate – Andy Tilston Sep 7 at 8:46
  • If you are able to de-serialize the json to a class, then use the getters of the class to extract the value of circuitId. In your case, since since Circuits is a list, you have to pass the index(0 in this case). – Kshetra Mohan Prusty Sep 7 at 8:49
  • Thanks Kshetra. Would you be able to provide an example of how this would look please? – Andy Tilston Sep 7 at 8:51

Assuming the de-serialized class for the json looks something like below, the way to extract "circuitId": "albert_park" is follows:

The code is not C#, but this can give an idea.

List<Circuit> circuitList = example.getCircuits();
circuitList.get(0).getCircuitId(); // this would return albert_park

De-serialized model class:

public class Example {

private String season;
private List<Circuit> circuits = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getSeason() {
return season;
}

public void setSeason(String season) {
this.season = season;
}

public List<Circuit> getCircuits() {
return circuits;
}

public void setCircuits(List<Circuit> circuits) {
this.circuits = circuits;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

There would be a similar class for Circuit having its attributes.

Let me show you a completely different path. We are using Newtonsoft.Json's method DeepEquals() to validate the responses returned (also using RestSharp, by the way).

This verifies all characters in the response, which has its disadvantages but also huge upsides. For example, you need never deserialize nor create classes to deserialize to. Keeps the code very dry.

Of course, for dealing with unique (unpredictable) values that you can't test, we wrote some extension methods to replace these values (by null or so) so DeepEquals doesn't get triggered by them.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.