Skip to content

Commit

Permalink
Register mocks as singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
xzxzxc committed Mar 31, 2023
1 parent 1eff21e commit 472c54a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Autofac.Extras.Moq/MoqRegistrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(
{
result = RegistrationBuilder.ForDelegate((c, p) => CreateMock(c, typedService))
.As(service)
.InstancePerLifetimeScope()
.SingleInstance()
.ExternallyOwned()
.CreateRegistration();
}
Expand Down
15 changes: 15 additions & 0 deletions test/Autofac.Extras.Moq.Test/AutoMockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ public void CreateNonGenericSameAsCreateGeneric()
}
}

[Fact]
public void ResolveInChildScope()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IDependency>().Setup(o => o.DoSomethingExternal()).Returns(1);
var rootScope = mock.Container;
var childScope = rootScope.BeginLifetimeScope();

var resolvedObject = childScope.Resolve<IDependency>();

Assert.Equal(1, resolvedObject.DoSomethingExternal());
}
}

public class ClassWithDependency
{
private readonly IDependency _dependency;
Expand Down

0 comments on commit 472c54a

Please sign in to comment.