1

I have been studying acceptance based tests and they look quite nice as they fit more naturally with feature based development.

The problem is that I do not know how to lay them out in code. I would like to try and avoid pulling in another framework to handle this so I am just looking for a simple way to get these tests up and running.

I am open to any changes needed to the code structure. I am also using specs to build up complex acceptance criteria.

Sample of what I am trying to do:

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));
    }
}

I may be completely off the mark here but essentially I would like to be able to run tests with different assumptions and givens. I don't mind if I have to make more classes or minor duplication as long as I can point someone to the test to validate a given criteria.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
deanvmc
  • 5,957
  • 6
  • 38
  • 68
  • 1
    This looks more like functional testing rather than acceptance testing (as you are simulating usage of your code by other code instead of usage of your program by a human). Is this correct? See [this question](http://en.wikipedia.org/wiki/Aspect-oriented_programming) for a good comparison of each. If so there are several methods which you could use, including an [aspect-oriented](http://en.wikipedia.org/wiki/Aspect-oriented_programming) approach. However I am no expert when it comes to acceptance testing, and wouldn't want to muddy the waters by mis-answering your question. – Ian T. Small Feb 05 '13 at 13:33
  • Honestly I am not sure. The idea is that a developer gave me a story for a http client and based on that story I need to fulfill some criteria based on a discussion we had about expectations. The end result of this is he gets what he wants and I code expectations. We are trying to find a way to only develop and test what is needed, no more no less. – deanvmc Feb 05 '13 at 13:43
  • 1
    PS your links both go to the same article. – deanvmc Feb 05 '13 at 13:43
  • This is absolutely a candidate for a `Gherkin` based acceptance test framework like `SpecFlow`. I know you said you didn't want to add a framework but this is precisely what they are designed for – levelnis Feb 05 '13 at 13:47
  • My concern is adding another level of complexity and getting approval for it. If it cannot be done without a framework I would questions the validity of the pattern to be honest. Unfortunatly I have to work with the confines present by my employer and adding more dependencies for a yet unproven (on our side) way of testing just wont happen. It could be an option down the road all the same so I will take a look at specflow too. – deanvmc Feb 05 '13 at 13:52
  • My apologies, the first link should have been [to this question](http://stackoverflow.com/questions/3370334/difference-between-acceptance-test-and-functional-test). – Ian T. Small Feb 06 '13 at 14:10

1 Answers1

1

I would strongly recommend using an ATDD framework like SpecFlow or even MSpec for creating tests of this nature. Implementing SpecFlow is then a case of writing your specification using domain specific language, in collaboration with domain expert(s) if that's approriate, and then satisfying the scenario steps defined in the feature through code. It's difficult to pad out the code aspect without understanding more about your exact requirements, but a sample feature might look something like this:

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 |

Then in your steps that are bound to the feature you can write the code that satisfies the specification steps. Here's one example:

[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();
    }
}
levelnis
  • 7,665
  • 6
  • 37
  • 61
  • Dammit... why did you go and provide a sample... Now I have to go all skunkworks and try this out because it looks stupidly cool.. this is all your fault.. – deanvmc Feb 05 '13 at 14:07
  • I'd say I'm sorry but I'm sincerely not - go ahead, you'll love it :) – levelnis Feb 05 '13 at 14:08
  • I've accepted. It works really well but I have found that we need to sit down and talk about what sort of testing we need and what we want out of it so I may have asked the wrong question. However when it comes to BDD we will be taking a serious look at specflow in the future. Cheers. – deanvmc Feb 05 '13 at 17:25