【问题标题】:Specflow read Json responseSpecflow 读取 Json 响应
【发布时间】:2019-03-14 14:12:12
【问题描述】:

需要测试以 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);
    }

如何解析这个?

【问题讨论】:

  • 这并不是 Gherkin 的真正用途。老实说,我只会用 C# 编写这些测试,而忘记 Gherkin 语言。

标签: automated-tests cucumber specflow gherkin


【解决方案1】:

简而言之: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 请求的响应保存在 Scenario 上下文中。

步骤:那么我应该会收到以下酒店平台:

进行简单的断言,并省略“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);
    }
}

【讨论】:

    【解决方案2】:

    改善这一点的一种方法是不在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 2020-09-13
      • 2013-09-09
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 2018-06-12
      相关资源
      最近更新 更多