Storyteller 5.1.0


Next

'Macros' with Paragraph Grammars

Previous

Asserting Facts

Asserting Values Edit on GitHub


It's just not a specification if you are not asserting that some value is an expected value, and Storyteller has you covered.

Sentence Methods

The classical approach is to assert on the value returned from a sentence method on a fixture like the example below:


[FormatAs("The value should be {value}")]
public double TheValueShouldBe()
{
    return _calculator.Value;
}

New to Storyteller 3.* is the ability to assert against C# output parameters so that you can make multiple assertions from one Sentence like this example below:


[FormatAs("For X={X} and Y={Y}, the Sum should be {Sum} and the Product should be {Product}")]
public void SumAndProduct(int X, int Y, out int Sum, out int Product)
{
    Sum = X + Y;
    Product = X * Y;
}

Equality Checking

At this time, Storyteller does a simple equality check using the value returned from the Sentence method compared against the expected value specified as a string in the specification converted to the same type as the value returned from the method.

Out of the box, Storyteller has a couple means of doing equality checks in order of precedence:

  1. For an array, check that the length of the actual value is the same as the expected value and that the element at each position matches
  2. For a List<T>, do a similar check
  3. Lastly, fall through to using expected.Equals(actual)

Formatting

By default, the return value of an assertion sentence grammar is returnValue, but you can customize this to something more meaningful in two ways:

  1. Decorate the return value with an alias attribute like [return: AliasAs("sum")]
  2. Specify the desired name of the return value cell in the sentence format as the last cell. In this case Storyteller will use the last value that does not match any of the input parameters of the method as the cell name of the return value.

Decision Tables

Any assertion sentence grammar can be turned into a tabular decision tree by decorating the method with an [ExposeAsTable("the title")] attribute like this example below:


[ExposeAsTable("Sum and Product Operations")]
public void Operations(int X, int Y, out int Sum, out int Product)
{
    Sum = X + Y;
    Product = X*Y;
}

See Tables for more information.

Assertions in Action

The results of a specification using all of the assertion grammars shown above will look like this: