Need to test GET that have Json as a response. I haven't found useful info in official documentation.
Feature: API_Retrieving_Platforms
As an authorized user...
@mytag
Scenario: Perform Get request
Given I am an authorized user
When I perform GET request "/api/hotels/lists/platforms",
Then I receive a JSON in response:
"""
[
{
"refId": 1,
"label": "Mobile"
},
{
"refId": 2,
"label": "Desktop"
}
]
"""
The step for retrieving Json is:
[Then(@"I receive a JSON in response:")]
public void ThenIReceiveAJSONInResponse(string JSON)
{
Assert.Equal(HttpStatusCode.OK, _responseMessage.StatusCode);
}
One way to improve this is by not putting the exact JSON in the Specflow step. I'd suggest using something like
Then I receive a response that looks like 'myResponsefile.json'
You could then create step which processes the response and looks at a file in your repo to compare it with
[Then(@"I receive a response that looks like '(.*)'")]
public void IreceiveAJsonResponse(string responsefilenametocompare)
{
string receivedjson = GetMyReceivedjsonObject();
string filePathAndName = "myfile.json";
string json = File.ReadAllText(filePathAndName);
JToken expected = JToken.Parse(json);
JToken actual = JToken.Parse(receivedjson);
actual.Should().BeEquivalentTo(expected);
}