Skip to content

Commit

Permalink
Autoactivate substituted values (#61)
Browse files Browse the repository at this point in the history
Without this, the values may not get resolved correctly as the timing
wouldn't be correct.
  • Loading branch information
twsouthwick authored Oct 28, 2020
1 parent 6574ca9 commit 6cd3b02
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions AutofacContrib.NSubstitute.Tests/SubstituteForFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,57 @@ namespace AutofacContrib.NSubstitute.Tests
[TestFixture]
public class SubstituteForFixture
{
[Test]
public void ConfigureSubstitueFor()
{
using var mock = AutoSubstitute.Configure()
.SubstituteFor<Concrete1>()
.ConfigureSubstitute((c, ctx) =>
{
var concrete2 = ctx.Resolve<Concrete2>();
c.Configure().Get().Returns(concrete2);
})
.SubstituteFor<Concrete2>()
.ConfigureSubstitute((c, ctx) =>
{
var concrete3 = ctx.Resolve<Concrete3>();
c.Configure().Get().Returns(concrete3);
})
.SubstituteFor<Concrete3>()
.Build();

var result = mock.Resolve<ConcreteTest>().Get();
var expected = mock.Resolve<Concrete3>();

Assert.AreSame(expected, result);
}

public abstract class Concrete1
{
public abstract Concrete2 Get();
}

public abstract class Concrete2
{
public abstract Concrete3 Get();
}

public abstract class Concrete3
{
}

public class ConcreteTest
{
private readonly Concrete2 _c2;

public ConcreteTest(Concrete1 c1)
{
_c2 = c1.Get();
}

public Concrete3 Get() => _c2.Get();
}

[Test]
[Obsolete]
public void Example_test_with_substitute_for_concrete_obsolete()
Expand Down
1 change: 1 addition & 0 deletions AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ private SubstituteForBuilder<TService> CreateSubstituteForBuilder<TService>(Func

var registration = _builder.Register(_ => factory())
.As<TService>()
.AutoActivate()
.InstancePerLifetimeScope();
var builder = new SubstituteForBuilder<TService>(this, registration, isSubstituteFor);

Expand Down

0 comments on commit 6cd3b02

Please sign in to comment.