SpecsFor lifecycle

Your spec goes through several different phases as it executes. Many of these phases can be overridden.

Extension Points

While just about every step of the SpecsFor lifecycle can be overridden, you typically will only override one of the following points:

  • ConfigureContainer - You can override this method to change the configuration of the auto-mocking container. You can register a concrete type, load registries, etc.
  • InitializeClassUnderTest - Sometimes you don't want the auto-mocking container to create your System Under Test for you. In these cases, you can override the InitializeClassUnderTest method and assign the SUT property yourself.
  • Given - You can override the Given method to establish state and context.
  • When - You will typically override this method to invoke some action on your System Under Test.
  • BeforeEachTest - This method can be overridden when you want to execute some logic before each individual test case within a spec has executed.
  • AfterEachTest - This method can be overridden when you want to execute some logic after each individual test case within a spec has executed.
  • AfterSpec - This method is called after all your test cases within a spec have executed. You can perform any manual cleanup here.

SetupEachSpec

First, SpecsFor executes the SetupEachSpec method. This method prepares your spec for execution by initializing the auto-mocking container, then creating your System Under Test (SUT).

Next, it executes the Given method. This method has no behavior by default, but you can override it to establish context.

Finally, the When method is executed. Again, this method has no behavior by default, but you will typically override this method and perform actions against your SUT.

Then...

Now that your spec has been initialized and the Given and When methods have executed, it's time for your 'Then' test cases to execute. Each test case is executed, followed by the AfterEachTest method. By default, the AfterEachTest method has no behavior, but you can override this method and provide your own behavior if you need to.

TearDown

At this point, your specs have finished executing, so its time to clean up. The AfterSpec method is called first. You can override this method to provide your own cleanup logic, if needed.

Finally, your spec is disposed. If the SUT implements the IDisposable interface, it is also disposed.

This process repeats for each spec you create by deriving from the SpecsFor class!