Autofac.Extras.NSubstitute 1.1.0

Autofac extension supporting generation of NSubstitute objects.

Install-Package Autofac.Extras.NSubstitute -Version 1.1.0
dotnet add package Autofac.Extras.NSubstitute --version 1.1.0
<PackageReference Include="Autofac.Extras.NSubstitute" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Autofac.Extras.NSubstitute --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Autofac.Extras.NSubstitute

NSubstitute auto mocking integration for Autofac Autofac IoC.

Getting Started

Given you have a system under test and a dependency:

public class SystemUnderTest
{
  public SystemUnderTest(IDependency dependency)
  {
  }
}

public interface IDependency
{
}

Use the Autofac.Extras.NSubstitute package's AutoSubstitute class to instantiate the system under test.

[Test]
public void Test()
{
  using (var as = new AutoSubstitute())
  {
    // The AutoSubstitute class will inject a mock IDependency
    // into the SystemUnderTest constructor
    var sut = as.Resolve<SystemUnderTest>();
  }
}

Configuring Mocks

[Test]
public void Test()
{
  using (var as = new AutoSubstitute())
  {
    // Arrange
    as.Resolve<IDependency>().GetValue().Returns("expected value");
    var sut = as.Resolve<SystemUnderTest>();

    // Act
    var actual = sut.DoWork();

    // Assert
    as.Resolve<IDependency>().Received().GetValue();
    Assert.AreEqual("expected value", actual);
  }
}

public class SystemUnderTest
{
  private readonly IDependency dependency;

  public SystemUnderTest(IDependency strings)
  {
    this.dependency = strings;
  }

  public string DoWork()
  {
    return this.dependency.GetValue();
  }
}

public interface IDependency
{
  string GetValue();
}

Configuring Specific Dependencies

You can configure the AutoSubstitute to provide a specific instance for a given service type:

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = new Dependency();
    as.Provide<IDependency>(dependency);

    // ...and the rest of the test.
  }
}

Partial subs

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = Substitute.ForPartsOf<Dependency>();
    dependency.When(x => x.GetValue()).DoNotCallBase();
    dependency.GetValue().Returns("1,2,3");
    as.Provide<IDependency>(dependency);

    // ...and the rest of the test.
  }
}

Or you can use the built in one

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = as.ProvidePartsOf<IDependency, Dependency>();
    dependency.When(x => x.GetValue()).DoNotCallBase();
    dependency.GetValue().Returns("1,2,3");

    // ...and the rest of the test.
  }
}

Autofac.Extras.NSubstitute

NSubstitute auto mocking integration for Autofac Autofac IoC.

Getting Started

Given you have a system under test and a dependency:

public class SystemUnderTest
{
  public SystemUnderTest(IDependency dependency)
  {
  }
}

public interface IDependency
{
}

Use the Autofac.Extras.NSubstitute package's AutoSubstitute class to instantiate the system under test.

[Test]
public void Test()
{
  using (var as = new AutoSubstitute())
  {
    // The AutoSubstitute class will inject a mock IDependency
    // into the SystemUnderTest constructor
    var sut = as.Resolve<SystemUnderTest>();
  }
}

Configuring Mocks

[Test]
public void Test()
{
  using (var as = new AutoSubstitute())
  {
    // Arrange
    as.Resolve<IDependency>().GetValue().Returns("expected value");
    var sut = as.Resolve<SystemUnderTest>();

    // Act
    var actual = sut.DoWork();

    // Assert
    as.Resolve<IDependency>().Received().GetValue();
    Assert.AreEqual("expected value", actual);
  }
}

public class SystemUnderTest
{
  private readonly IDependency dependency;

  public SystemUnderTest(IDependency strings)
  {
    this.dependency = strings;
  }

  public string DoWork()
  {
    return this.dependency.GetValue();
  }
}

public interface IDependency
{
  string GetValue();
}

Configuring Specific Dependencies

You can configure the AutoSubstitute to provide a specific instance for a given service type:

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = new Dependency();
    as.Provide<IDependency>(dependency);

    // ...and the rest of the test.
  }
}

Partial subs

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = Substitute.ForPartsOf<Dependency>();
    dependency.When(x => x.GetValue()).DoNotCallBase();
    dependency.GetValue().Returns("1,2,3");
    as.Provide<IDependency>(dependency);

    // ...and the rest of the test.
  }
}

Or you can use the built in one

[Test]
public void Test()
{
  using (var as = AutoSubstitute())
  {
    var dependency = as.ProvidePartsOf<IDependency, Dependency>();
    dependency.When(x => x.GetValue()).DoNotCallBase();
    dependency.GetValue().Returns("1,2,3");

    // ...and the rest of the test.
  }
}

Release Notes

Release notes are at https://github.com/zskovacs/Autofac.Extras.NSubstitute/releases

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.0 6,096 10/12/2018
1.0.0 158 10/9/2018