Skip to content
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
11 changes: 11 additions & 0 deletions Moq.AutoMock.Tests/Resolvers/SelfResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public void WhenRequestTypeIsIServiceProviderThenResolvesToAutoMockerInstance()
Assert.IsTrue(context.ValueProvided);
Assert.AreSame(mocker, context.Value);
}

[TestMethod]
[Description("Issue 450")]
public void WhenServiceProviderIsRequestedAsMockThenItReturnsMockedServiceProvider()
{
var mocker = new AutoMocker();

var serviceProvider = mocker.GetMock<IServiceProvider>();

Assert.IsNotNull(serviceProvider);
}
}
2 changes: 1 addition & 1 deletion Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public Mock GetMock(Type serviceType, bool enablePrivate)

private Mock GetMockImplementation(Type serviceType, bool enablePrivate)
{
if (TryResolve(serviceType, new ObjectGraphContext(enablePrivate), out IInstance? instance, out bool noCache) &&
if (TryResolve(serviceType, new ObjectGraphContext(enablePrivate, isMockCreation:true), out IInstance? instance, out bool noCache) &&
instance.IsMock)
{
if (!noCache && TypeMap is { } typeMap && !typeMap.ContainsKey(serviceType))
Expand Down
19 changes: 18 additions & 1 deletion Moq.AutoMock/ObjectGraphContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ public class ObjectGraphContext
/// <summary>
/// Creates an instance with binding flags set according to `enablePrivate`.
/// </summary>
/// <param name="enablePrivate"></param>
/// <param name="enablePrivate">Enable private members</param>
public ObjectGraphContext(bool enablePrivate)
{
BindingFlags = GetBindingFlags(enablePrivate);
VisitedTypes = [];
}

/// <summary>
/// Creates an instance with binding flags set according to `enablePrivate`.
/// </summary>
/// <param name="enablePrivate">Enable private members</param>
/// <param name="isMockCreation">Indicates if the object is for a mock instance.</param>
public ObjectGraphContext(bool enablePrivate, bool isMockCreation)
: this(enablePrivate)
{
IsMockCreation = isMockCreation;
}

/// <summary>
/// Creates a new instance, copying the values for the Binding flags and the Visited types.
/// </summary>
Expand Down Expand Up @@ -46,6 +57,12 @@ public ObjectGraphContext(ObjectGraphContext context, ParameterInfo targetParame
ParameterInfo = targetParameter ?? throw new ArgumentNullException(nameof(targetParameter));
}

/// <summary>
/// Indicates if the requested operation is for creating a Mock instance.
/// This will will only be true for types requested through AutoMocker.GetMock methods.
/// </summary>
public bool IsMockCreation { get; }

/// <summary>
/// Flags passed to Mock constructor.
/// </summary>
Expand Down
12 changes: 11 additions & 1 deletion Moq.AutoMock/Resolvers/SelfResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
public class SelfResolver : SimpleTypeResolver<AutoMocker>
{
/// <inheritdoc />
protected override AutoMocker GetValue(MockResolutionContext context)
public override void Resolve(MockResolutionContext context)
{
if (context.ObjectGraphContext.IsMockCreation)
{
return;
}
base.Resolve(context);
}

/// <inheritdoc />
protected override AutoMocker GetValue(MockResolutionContext context)
=> context.AutoMocker;
}
2 changes: 1 addition & 1 deletion Moq.AutoMock/Resolvers/SimpleTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class SimpleTypeResolver<T> : IMockResolver
protected bool IncludeInterfaces { get; set; } = true;

/// <inheritdoc />
public void Resolve(MockResolutionContext context)
public virtual void Resolve(MockResolutionContext context)
{
if (context.RequestType == typeof(T) ||
(IncludeBaseTypes && typeof(T).IsAssignableFrom(context.RequestType)) ||
Expand Down
Loading