Skip to content

Commit 127c7f5

Browse files
authored
Make MockHandler method protected (#46)
1 parent 3584a43 commit 127c7f5

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

AutofacContrib.NSubstitute/AutoSubstituteOptionsExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private NonPublicConstructorFinder()
5454

5555
private class AutoPropertyInjectorMockHandler : MockHandler
5656
{
57-
public override void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
57+
protected internal override void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
5858
{
5959
var router = substitutionContext.GetCallRouterFor(instance);
6060

AutofacContrib.NSubstitute/MockHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ protected MockHandler()
2020
/// <param name="type">The type the mock was created for.</param>
2121
/// <param name="context">The current component context.</param>
2222
/// <param name="substitutionContext">The current substitution context.</param>
23-
public abstract void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext);
23+
protected internal abstract void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext);
2424
}
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Autofac;
2+
using NSubstitute.Core;
3+
using System;
4+
5+
namespace AutofacContrib.NSubstitute
6+
{
7+
/// <summary>
8+
/// A handler that is used to modify any of the auto-generated NSubstitute mocks when the type is <typeparamref name="T"/>.
9+
/// </summary>
10+
public abstract class MockHandler<T> : MockHandler
11+
where T : class
12+
{
13+
/// <inheritdoc />
14+
protected internal sealed override void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
15+
{
16+
if (typeof(T) == type && instance is T t)
17+
{
18+
OnMockCreated(t, context, substitutionContext);
19+
}
20+
}
21+
22+
/// <summary>
23+
/// Gets the type associated with this handler.
24+
/// </summary>
25+
protected Type Type => typeof(T);
26+
27+
/// <summary>
28+
/// Provides a way to manage mocks after creation but before returned from the container registry.
29+
/// </summary>
30+
/// <param name="instance">The mock instance.</param>
31+
/// <param name="context">The current component context.</param>
32+
/// <param name="substitutionContext">The current substitution context.</param>
33+
protected abstract void OnMockCreated(T instance, IComponentContext context, ISubstitutionContext substitutionContext);
34+
}
35+
}

0 commit comments

Comments
 (0)