1

我一直在研究基于验收的测试,它们看起来很不错,因为它们更自然地适合基于特性的开发。

问题是我不知道如何在代码中布置它们。我想尽量避免引入另一个框架来处理这个问题,所以我只是在寻找一种简单的方法来启动和运行这些测试。

我对代码结构所需的任何更改持开放态度。我还使用规范来建立复杂的验收标准。

我正在尝试做的示例:

public class When_a_get_request_is_created
{
    private readonly IHttpRequest _request;

    public When_a_get_request_is_created()
    {
        _request = new HttpRequest();
    }

    // How to call this?
    public void Given_the_method_assigned_is_get()
    {
        _request = _request.AsGet();
    }

    // What about this?
    public void Given_the_method_assigned_is_not_get()
    {
        _request = _request.AsPost();
    }

    // It would be great to test different assumptions.
    public void Assuming_default_headers_have_been_added()
    {
        _request = _request.WithDefaultHeaders();
    }

    [Fact]
    public void It_Should_Satisfy_RequestIsGetSpec()
    {
        Assert.True(new RequestUsesGetMethodSpec().IsSatisfiedBy(_request));
    }
}

我可能在这里完全不合时宜,但基本上我希望能够运行具有不同假设和给定的测试。只要我可以将某人指向测试以验证给定的标准,我不介意是否必须进行更多课程或少量重复。

4

1 回答 1

1

我强烈建议使用 ATDD 框架SpecFlow,甚至MSpec用于创建这种性质的测试。然后,实施SpecFlow就是使用特定领域的语言编写规范,如果合适的话,与领域专家合作,然后通过代码满足功能中定义的场景步骤。如果不了解更多关于您的确切要求,很难填充代码方面,但示例功能可能如下所示:

Feature: HTTP Requests
    In order to validate that HTTP requests use the correct methods
    As a client application
    I want specific requests to use specific methods

Scenario Outline: Making HTTP Requests
    Given I am making a request
    When the method assigned is <Method>
    And the <Header> header is sent
    Then it should satisfy the requirement for a <Method> request

Examples:
| Method | Header   |
| Get    | Header 1 |
| Get    | Header 2 |
| Get    | Header 3 |
| Post   | Header 1 |

然后在绑定到该功能的步骤中,您可以编写满足规范步骤的代码。这是一个例子:

[Binding]
public class HttpRequestSteps
{
    [When(@"the method assigned is (.*)")]
    public void WhenTheMethodAssignedIs(string method)
    {
        // not sure what this should be returning, but you can store it in ScenarioContext and retrieve it in a later step binding by casting back based on key
        // e.g. var request = (HttpRequest)ScenarioContext.Current["Request"]
        ScenarioContent.Current["Request"] = _request.AsGet();
    }
}
于 2013-02-05T14:01:17.457 回答