Skip to content

Commit

Permalink
Merge pull request #46 from xzxzxc/singleton_mocks
Browse files Browse the repository at this point in the history
Register mocks as singletons
  • Loading branch information
tillig authored Mar 31, 2023
2 parents 1eff21e + 472c54a commit 39a2693
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 39a2693

Please sign in to comment.