Specflow 读取 Json 响应
Specflow read Json response
需要测试具有 Json 作为响应的 GET。我没有在官方文档中找到有用的信息。
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"
}
]
"""
检索Json的步骤是:
[Then(@"I receive a JSON in response:")]
public void ThenIReceiveAJSONInResponse(string JSON)
{
Assert.Equal(HttpStatusCode.OK, _responseMessage.StatusCode);
}
如何解析这个?
改进这一点的一种方法是不在 Specflow 步骤中放置确切的 JSON。
我建议使用类似
的东西
Then I receive a response that looks like 'myResponsefile.json'
然后您可以创建步骤来处理响应并查看存储库中的文件以将其与
[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);
}
简而言之:You're Cuking It Wrong.
较长的答案是您需要以不同的方式编写步骤,以便从中删除技术术语,并专注于业务价值。
Scenario: Retriving a list of platforms
Given I am an authorized user
When I retrieve a list of hotel platforms
Then I should receive the following hotel platforms:
| Platform |
| Mobile |
| Desktop |
步骤:当我检索酒店平台列表时
此步骤应使用 C# 代码发出 GET 请求。在场景上下文中保存该 GET 请求的响应。
步骤:那我应该会收到以下酒店平台:
做一个简单的断言,并省略像"Ref Id"这样的技术信息。平台名称才是您真正关心的。
这些步骤的粗略开始是:
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
[Binding]
public class PlatformSteps
{
private readonly ScenarioContext scenario;
/// <summary>
/// Gets or sets the hotel platforms on the scenario context
/// </summary>
private IEnumerable<SomeJsonResponseType> HotelPlatforms
{
get => (IEnumerable<SomeJsonResponseType>)scenario["HotelPlatformsResponse"];
set => scenario["HotelPlatformsResponse"] = value;
}
public PlatformSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[When(@"^When I retrieve a list of hotel platforms")]
public void WhenIRetrieveAListOfHotelPlatforms()
{
HotelPlatforms = api.GetHotelPlatforms(); // Or whatever you API call looks like
}
[Then(@"^I should receive the following hotel platforms:")]
public void IShouldReceiveTheFollowingHotelPlatforms(Table table)
{
var actualPlatforms = HotelPlatforms.Select(r => r.PlatformName);
table.CompareToSet(actualPlatforms);
}
}
需要测试具有 Json 作为响应的 GET。我没有在官方文档中找到有用的信息。
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"
}
]
"""
检索Json的步骤是:
[Then(@"I receive a JSON in response:")]
public void ThenIReceiveAJSONInResponse(string JSON)
{
Assert.Equal(HttpStatusCode.OK, _responseMessage.StatusCode);
}
如何解析这个?
改进这一点的一种方法是不在 Specflow 步骤中放置确切的 JSON。 我建议使用类似
的东西Then I receive a response that looks like 'myResponsefile.json'
然后您可以创建步骤来处理响应并查看存储库中的文件以将其与
[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);
}
简而言之:You're Cuking It Wrong.
较长的答案是您需要以不同的方式编写步骤,以便从中删除技术术语,并专注于业务价值。
Scenario: Retriving a list of platforms
Given I am an authorized user
When I retrieve a list of hotel platforms
Then I should receive the following hotel platforms:
| Platform |
| Mobile |
| Desktop |
步骤:当我检索酒店平台列表时
此步骤应使用 C# 代码发出 GET 请求。在场景上下文中保存该 GET 请求的响应。
步骤:那我应该会收到以下酒店平台:
做一个简单的断言,并省略像"Ref Id"这样的技术信息。平台名称才是您真正关心的。
这些步骤的粗略开始是:
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
[Binding]
public class PlatformSteps
{
private readonly ScenarioContext scenario;
/// <summary>
/// Gets or sets the hotel platforms on the scenario context
/// </summary>
private IEnumerable<SomeJsonResponseType> HotelPlatforms
{
get => (IEnumerable<SomeJsonResponseType>)scenario["HotelPlatformsResponse"];
set => scenario["HotelPlatformsResponse"] = value;
}
public PlatformSteps(ScenarioContext scenario)
{
this.scenario = scenario;
}
[When(@"^When I retrieve a list of hotel platforms")]
public void WhenIRetrieveAListOfHotelPlatforms()
{
HotelPlatforms = api.GetHotelPlatforms(); // Or whatever you API call looks like
}
[Then(@"^I should receive the following hotel platforms:")]
public void IShouldReceiveTheFollowingHotelPlatforms(Table table)
{
var actualPlatforms = HotelPlatforms.Select(r => r.PlatformName);
table.CompareToSet(actualPlatforms);
}
}