Skip to content

Allow a way to set default options to be used for a whole test suite #49

@twsouthwick

Description

@twsouthwick

There are now a number of options that can be set (and infinite combinations with custom MockHandlers). Some of these I find I need to use in the same way for an entire test suite. Currently, I'm using a custom extension method, but the drawback is it has to be called on every builder, ie:

internal static class MyCustomAutoSubstituteExtensions
{
  public static AutoSubstituteBuilder AddCustomExtensions(this AutoSubstituteBuilder builder) => ...
}

public class Test
{
  [Fact]
  public void SomeTest()
  {
    using var mock = AutoSubstitute.Configure()
      .AddCustomExtensions()
      ...
      .Build();
}

This works, but requires every test to manually call this. I propose an API that can be called early on (potentially via C# 9 module initializers) that then gets applied to all tests. It would also be good to provide a way to 'lock' the default set so nothing else inadvertently changes things.

Here's the proposal for the API:

public class AutoSubstitute
{
  public static DefaultAutoSubstituteBuilder Default { get; }
}

public class DefaultAutoSubstituteBuilder
{
  public DefaultAutoSubstituteBuilder ConfigureDefault(Action<AutoSubstituteBuilder> configure);
  public void Reset();
  public DefaultAutoSubstituteBuilder Lock();
}

Some open questions:

  • Should we allow default registrations to exist? This isn't supported directly by Autofac, but we can work something around (ie maintain a list of methods to call to register)
  • Some methods on AutoSubstituteBuilder will need to be made virtual (and maybe on the SubstituteForBuilder) to gather the submitted configurations. Can we minimize this so that everything funnels through just a couple of methods that can be virtualized to accomplish this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions