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
-
1This could be a dup or extension of of stackoverflow.com/questions/25052293/….Kshetra Mohan Prusty– Kshetra Mohan Prusty2018年09月07日 08:44:56 +00:00Commented Sep 7, 2018 at 8:44
-
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– Kshetra Mohan Prusty2018年09月07日 08:49:56 +00:00Commented Sep 7, 2018 at 8:49
-
Thanks Kshetra. Would you be able to provide an example of how this would look please?Andy Tilston– Andy Tilston2018年09月07日 08:51:31 +00:00Commented Sep 7, 2018 at 8:51
2 Answers 2
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.
Explore related questions
See similar questions with these tags.