1. Johannes Rudolph
  2. SubSpec

Wiki

Clone wiki

SubSpec / Recommended Syntax

[Specification]
public void PushSpecification()
{
    var sut = default( Stack<int> );    // Capture the System under test in a var called sut, initialized to the default value for that type.
    "Given a new stack"                 // descriptions go on single line
        .Context( () =>                 // actions go on single line with lambda, indent by one tab
            sut = new Stack<int>() );   // single line delegates fit on separate line, indent by two tabs

    var result = default( int );        // capture results in a var called result, initialized to the default value for that type.
    "with an element pushed onto it"    // same for Do
        .Do( () => 
            result = sut.Push( 11 ) );

    /* Do block alternative for capturing exceptions */
    var exception = default( Exception ); // capture exception in a var called exception
    "with an element pushed onto it"      // same for Do
        .Do( () =>                        // use xUnit's Record facility to capture the exception  
            exception = Record.Exception( () => 
                sut.Push( int.Parse( "nonumber" ) ) ) ); 

    "the stack's Top is the pushed element"
        .Assert( () =>                  // for multiline lambda blocks, align brace with .Action, 
        {
            var top = sut.Peek();       // code is indented by two tabs, just as with single line delegates
            Assert.Equals( 11, top );
        } );   
}

This syntax has the advantage of being automatically supported and enforced by Visual Studios "Format Document" action.

Updated