Verifying with 'Then'

In the Given, When, Then setup, the Then step is for you that the action you performed against the System Under Test (SUT) had the desired outcome(s).

Consider our simple spec again:

πŸ“˜

Given a car is started, when the car is stopped, then the engine is stopped.

Unlike Given and When, there is no Then method to override in SpecsFor. Instead, you create test cases for each outcome you wish to verify, like so:

[Test]
public void then_the_engine_is_stopped()
{
  //Then: the engine is stopped.
  SUT.Engine.IsStopped.ShouldBeTrue();
}

🚧

BE CAREFUL!

Be sure you decorate each of your test cases with the NUnit Test attribute. If you don't, NUnit won't see your test cases, and they won't be executed.

The above spec only had a single outcome to verify, but it's not uncommon for specs to have multiple things to check:

πŸ“˜

Given a car is started, when the car is stopped..
then the car is stopped.
then the engine is stopped.

The full SpecsFor implementation of this spec would look like this:

public class given_a_car_is_started_when_a_car_is_stopped : SpecsFor<Car>
{
  protected override void Given()
  {
    //Given: establish state, in this case, that the car is started.
    SUT.Start();
  }

  protected override void When()
  {
    //When: perform an action, in this case, stopping the car.
    SUT.Stop();
  }

  //Note that there can be (and usually are) multiple 'Then' test cases.
  [Test]
  public void then_the_car_is_stopped()
  {
    //Then: the car is stopped.
    SUT.IsStopped.ShouldBeTrue();
  }

  [Test]
  public void then_the_engine_is_stopped()
  {
    //Then: the engine is stopped.
    SUT.Engine.IsStopped.ShouldBeTrue();
  }
}