From 6cd3b02b15e5254a182cba9c0d2d304f6402fcf8 Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Wed, 28 Oct 2020 12:50:46 -0700 Subject: [PATCH] Autoactivate substituted values (#61) Without this, the values may not get resolved correctly as the timing wouldn't be correct. --- .../SubstituteForFixture.cs | 51 +++++++++++++++++++ .../AutoSubstituteBuilder.cs | 1 + 2 files changed, 52 insertions(+) diff --git a/AutofacContrib.NSubstitute.Tests/SubstituteForFixture.cs b/AutofacContrib.NSubstitute.Tests/SubstituteForFixture.cs index d51210b..811ef54 100644 --- a/AutofacContrib.NSubstitute.Tests/SubstituteForFixture.cs +++ b/AutofacContrib.NSubstitute.Tests/SubstituteForFixture.cs @@ -9,6 +9,57 @@ namespace AutofacContrib.NSubstitute.Tests [TestFixture] public class SubstituteForFixture { + [Test] + public void ConfigureSubstitueFor() + { + using var mock = AutoSubstitute.Configure() + .SubstituteFor() + .ConfigureSubstitute((c, ctx) => + { + var concrete2 = ctx.Resolve(); + c.Configure().Get().Returns(concrete2); + }) + .SubstituteFor() + .ConfigureSubstitute((c, ctx) => + { + var concrete3 = ctx.Resolve(); + c.Configure().Get().Returns(concrete3); + }) + .SubstituteFor() + .Build(); + + var result = mock.Resolve().Get(); + var expected = mock.Resolve(); + + 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() diff --git a/AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs b/AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs index 57a111b..47d012e 100644 --- a/AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs +++ b/AutofacContrib.NSubstitute/AutoSubstituteBuilder.cs @@ -267,6 +267,7 @@ private SubstituteForBuilder CreateSubstituteForBuilder(Func var registration = _builder.Register(_ => factory()) .As() + .AutoActivate() .InstancePerLifetimeScope(); var builder = new SubstituteForBuilder(this, registration, isSubstituteFor);