Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non generic create #44

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Autofac.Extras.Moq/AutoMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ public T Create<T>(params Parameter[] parameters)
return Create<T>(false, parameters);
}

/// <summary>
/// Resolve the specified type in the container (register it if needed).
/// </summary>
/// <param name="serviceType">Type of service.</param>
/// <param name="parameters">Optional parameters.</param>
/// <returns>The service.</returns>
public object Create(Type serviceType, params Parameter[] parameters)
{
return Create(false, serviceType, parameters);
}

/// <summary>
/// Verifies mocks and disposes internal container.
/// </summary>
Expand All @@ -197,18 +208,23 @@ public Mock<T> Mock<T>(params Parameter[] parameters)
return obj.Mock;
}

private T Create<T>(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<T>(parameters);
return this.Container.Resolve(serviceType, parameters);
}

private T Create<T>(bool isMock, params Parameter[] parameters)
{
return (T)Create(isMock, typeof(T), parameters);
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions test/Autofac.Extras.Moq.Test/AutoMockFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ public void MockedClassWithConstructorThrows()
}
}

[Fact]
public void CreateNonGenericSameAsCreateGeneric()
{
using (var mock = AutoMock.GetLoose())
{
var generic = mock.Create<ServiceA>();
var nonGeneric = mock.Create(typeof(ServiceA));

Assert.Same(generic, nonGeneric);
}
}

public class ClassWithDependency
{
private readonly IDependency _dependency;
Expand Down