From d7f6ac5066df7fd424ce9cba07fc8a809b697451 Mon Sep 17 00:00:00 2001 From: Kevin Driedger Date: Tue, 19 Oct 2021 12:38:11 -0400 Subject: [PATCH 1/2] allow non generic automock creation --- src/Autofac.Extras.Moq/AutoMock.cs | 24 +++++++++++++++---- .../AutoMockFixture.cs | 12 ++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Autofac.Extras.Moq/AutoMock.cs b/src/Autofac.Extras.Moq/AutoMock.cs index 6f66324..a51911e 100644 --- a/src/Autofac.Extras.Moq/AutoMock.cs +++ b/src/Autofac.Extras.Moq/AutoMock.cs @@ -175,6 +175,17 @@ public T Create(params Parameter[] parameters) return Create(false, parameters); } + /// + /// Resolve the specified type in the container (register it if needed). + /// + /// type of service + /// Optional parameters. + /// The service. + public object Create(Type serviceType, params Parameter[] parameters) + { + return Create(false, serviceType, parameters); + } + /// /// Verifies mocks and disposes internal container. /// @@ -197,18 +208,23 @@ public Mock Mock(params Parameter[] parameters) return obj.Mock; } - private T Create(bool isMock, params Parameter[] parameters) + private object Create(bool isMock, Type serviceType, params Parameter[] parameters) { if (isMock) { - _mockedServiceTypes.Add(typeof(T)); + _mockedServiceTypes.Add(serviceType); } else { - _createdServiceTypes.Add(typeof(T)); + _createdServiceTypes.Add(serviceType); } - return this.Container.Resolve(parameters); + return this.Container.Resolve(serviceType, parameters); + } + + private T Create(bool isMock, params Parameter[] parameters) + { + return (T)Create(isMock, typeof(T), parameters); } /// diff --git a/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs b/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs index 6775be4..81a7826 100644 --- a/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs +++ b/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs @@ -352,6 +352,18 @@ public void MockedClassWithConstructorThrows() } } + [Fact] + public void CreateNonGenericSameAsCreateGeneric() + { + using(var mock = AutoMock.GetLoose()) + { + var generic = mock.Create(); + var nonGeneric = mock.Create(typeof(ServiceA)); + + Assert.Same(generic, nonGeneric); + } + } + public class ClassWithDependency { private readonly IDependency _dependency; From 201bfe612d90deb4a3ca1bb69fe857681ff439c3 Mon Sep 17 00:00:00 2001 From: Kevin Driedger Date: Tue, 19 Oct 2021 14:02:08 -0400 Subject: [PATCH 2/2] a few small refactors --- src/Autofac.Extras.Moq/AutoMock.cs | 4 ++-- test/Autofac.Extras.Moq.Test/AutoMockFixture.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Autofac.Extras.Moq/AutoMock.cs b/src/Autofac.Extras.Moq/AutoMock.cs index a51911e..26b0a76 100644 --- a/src/Autofac.Extras.Moq/AutoMock.cs +++ b/src/Autofac.Extras.Moq/AutoMock.cs @@ -178,7 +178,7 @@ public T Create(params Parameter[] parameters) /// /// Resolve the specified type in the container (register it if needed). /// - /// type of service + /// Type of service. /// Optional parameters. /// The service. public object Create(Type serviceType, params Parameter[] parameters) @@ -208,7 +208,7 @@ public Mock Mock(params Parameter[] parameters) return obj.Mock; } - private object Create(bool isMock, Type serviceType, params Parameter[] parameters) + private object Create(bool isMock, Type serviceType, params Parameter[] parameters) { if (isMock) { diff --git a/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs b/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs index 81a7826..bfa009b 100644 --- a/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs +++ b/test/Autofac.Extras.Moq.Test/AutoMockFixture.cs @@ -355,7 +355,7 @@ public void MockedClassWithConstructorThrows() [Fact] public void CreateNonGenericSameAsCreateGeneric() { - using(var mock = AutoMock.GetLoose()) + using (var mock = AutoMock.GetLoose()) { var generic = mock.Create(); var nonGeneric = mock.Create(typeof(ServiceA));