Creating a configuration class

Before you can define conventions for your specs, you need to add a SpecsForConfiguration class to your spec project. In the root of your project (or in a subfolder if you only want the conventions to apply to a subset of your specs), create a new class named "Configuration." Make this class derive from the SpecsFor.SpecsForConfiguration class, and decorate it with the NUnit SetUpFixture attribute:

using NUnit.Framework;
using SpecsFor.Configuration;

namespace Conventions.Basics
{
    [SetUpFixture]
    public class Configuration : SpecsForConfiguration
    {
    }
}

You can use the helper methods on the base class to define your conventions. You'll do this within a constructor on your Configuration class.

There are three methods you can use to define your conventions:

  • WhenTestingAnything - Begins defining a convention that will apply to all of your SpecsFor specs. Note that any specs that do not derive from SpecsFor will not have this convention applied to them.
  • WhenTesting - Begins a convention that applies to all specs that satisfy a specified filter. For example: WhenTesting(s => s.Name.Contains("Foo"))
  • WhenTesting - Begins a convention that applies to all specs that can be cast to type T.

Each of the above methods returns a fluent builder that you can use to either attach behaviors or customize how the spec's SUT is instantiated.

  • EnrichWith - Applies the specified behavior to specs matching the convention.
  • CreateClassUnderTestingUsing - Uses the specified factory function to create the SUT for specs matching the convention.

Continue on to see how to define a behavior that applies to all classes.