Skip to content

Commit

Permalink
Update nullable annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Oct 1, 2024
1 parent d737cf4 commit 01bf166
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Autofac.Extras.Moq/MoqRegistrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ internal class MoqRegistrationHandler : IRegistrationSource
private readonly ISet<Type> _createdServiceTypes;
private readonly ISet<Type> _mockedServiceTypes;

private readonly MethodInfo _createMethod;
/// <summary>
/// This is <see cref="MockFactory.Create{T}()"/> with zero parameters. This
/// is important because it limits what can be auto-mocked. (MockFactory got
/// renamed to MockRepository but the method reference is still internally
/// on MockFactory.)
/// </summary>
private readonly MethodInfo _createMethod = typeof(MockRepository).GetMethod(nameof(MockRepository.Create), Array.Empty<Type>()) ?? throw new NotSupportedException("Unable to bind to Create method.");

/// <summary>
/// Initializes a new instance of the <see cref="MoqRegistrationHandler"/> class.
Expand All @@ -30,11 +36,6 @@ public MoqRegistrationHandler(ISet<Type> createdServiceTypes, ISet<Type> mockedS
{
_createdServiceTypes = createdServiceTypes;
_mockedServiceTypes = mockedServiceTypes;

// This is MockRepository.Create<T>() with zero parameters. This is important because
// it limits what can be auto-mocked.
var factoryType = typeof(MockRepository);
_createMethod = factoryType.GetMethod(nameof(MockRepository.Create), Array.Empty<Type>());
}

/// <summary>
Expand Down Expand Up @@ -215,13 +216,13 @@ private object CreateMock(IComponentContext context, TypedService typedService)
try
{
var specificCreateMethod = _createMethod.MakeGenericMethod(new[] { typedService.ServiceType });
var mock = (Mock)specificCreateMethod.Invoke(context.Resolve<MockRepository>(), null);
var mock = (Mock)specificCreateMethod.Invoke(context.Resolve<MockRepository>(), null)!;
return mock.Object;
}
catch (TargetInvocationException ex)
{
// Expose the inner exception as if it was directly thrown.
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
ExceptionDispatchInfo.Capture(ex.InnerException!).Throw();

// Won't get here, but the compiler doesn't know that.
throw ex.InnerException;
Expand Down

0 comments on commit 01bf166

Please sign in to comment.