Skip to content

Commit fbcf3e2

Browse files
authored
Use MockCreatedContext for MockHandler (#59)
1 parent 7adc134 commit fbcf3e2

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

AutofacContrib.NSubstitute/MockHandlers/AutoPropertyInjectorMockHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ private AutoPropertyInjectorMockHandler()
1212
{
1313
}
1414

15-
protected internal override void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
15+
protected internal override void OnMockCreated(MockCreatedContext context)
1616
{
17-
var router = substitutionContext.GetCallRouterFor(instance);
17+
var router = context.SubstitutionContext.GetCallRouterFor(context.Instance);
1818

19-
router.RegisterCustomCallHandlerFactory(_ => new AutoPropertyInjectorCallHandler(context));
19+
router.RegisterCustomCallHandlerFactory(_ => new AutoPropertyInjectorCallHandler(context.Context));
2020
}
2121

2222
private class AutoPropertyInjectorCallHandler : ICallHandler
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Autofac;
2+
using NSubstitute.Core;
3+
using System;
4+
5+
namespace AutofacContrib.NSubstitute.MockHandlers
6+
{
7+
/// <summary>
8+
/// Context for a mock that has been created.
9+
/// </summary>
10+
public class MockCreatedContext
11+
{
12+
internal MockCreatedContext(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
13+
{
14+
Instance = instance;
15+
Type = type;
16+
Context = context;
17+
SubstitutionContext = substitutionContext;
18+
}
19+
20+
/// <summary>
21+
/// Gets the created mock instance.
22+
/// </summary>
23+
public object Instance { get; }
24+
25+
/// <summary>
26+
/// Gets the type that the mock is being used for.
27+
/// </summary>
28+
public Type Type { get; }
29+
30+
/// <summary>
31+
/// Gets the current resolve context.
32+
/// </summary>
33+
public IComponentContext Context { get; }
34+
35+
/// <summary>
36+
/// Gets the NSubstitute substitution context.
37+
/// </summary>
38+
public ISubstitutionContext SubstitutionContext { get; }
39+
}
40+
}

AutofacContrib.NSubstitute/MockHandler.cs renamed to AutofacContrib.NSubstitute/MockHandlers/MockHandler.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using AutofacContrib.NSubstitute.MockHandlers;
33
using NSubstitute.Core;
44
using System;
5+
using System.ComponentModel;
56

67
namespace AutofacContrib.NSubstitute
78
{
@@ -21,10 +22,23 @@ protected MockHandler()
2122
/// <param name="type">The type the mock was created for.</param>
2223
/// <param name="context">The current component context.</param>
2324
/// <param name="substitutionContext">The current substitution context.</param>
24-
protected internal virtual void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
25+
[Obsolete]
26+
[EditorBrowsable(EditorBrowsableState.Never)]
27+
protected virtual void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
2528
{
2629
}
2730

31+
/// <summary>
32+
/// Provides a way to manage mocks after creation but before returned from the container registry.
33+
/// </summary>
34+
/// <param name="context">Created context.</param>
35+
protected internal virtual void OnMockCreated(MockCreatedContext context)
36+
{
37+
#pragma warning disable CS0612 // Type or member is obsolete
38+
OnMockCreated(context.Instance, context.Type, context.Context, context.SubstitutionContext);
39+
#pragma warning restore CS0612 // Type or member is obsolete
40+
}
41+
2842
/// <summary>
2943
/// Provides a way to manage the initial creation of mocks. Defaults to creating for all types.
3044
/// </summary>
@@ -33,4 +47,4 @@ protected internal virtual void OnMockCreating(MockCreatingContext context)
3347
{
3448
}
3549
}
36-
}
50+
}

AutofacContrib.NSubstitute/MockHandler{T}.cs renamed to AutofacContrib.NSubstitute/MockHandlers/MockHandler{T}.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Autofac;
2+
using AutofacContrib.NSubstitute.MockHandlers;
23
using NSubstitute.Core;
34
using System;
45

@@ -11,11 +12,11 @@ public abstract class MockHandler<T> : MockHandler
1112
where T : class
1213
{
1314
/// <inheritdoc />
14-
protected internal sealed override void OnMockCreated(object instance, Type type, IComponentContext context, ISubstitutionContext substitutionContext)
15+
protected internal sealed override void OnMockCreated(MockCreatedContext context)
1516
{
16-
if (typeof(T) == type && instance is T t)
17+
if (typeof(T) == context.Type && context.Instance is T t)
1718
{
18-
OnMockCreated(t, context, substitutionContext);
19+
OnMockCreated(t, context.Context, context.SubstitutionContext);
1920
}
2021
}
2122

AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public IEnumerable<IComponentRegistration> RegistrationsFor
7979
var instance = Substitute.For(new[] { typedService.ServiceType }, null);
8080
var ctx = c.Resolve<IComponentContext>();
8181

82+
var mockCreatedContext = new MockCreatedContext(instance, typedService.ServiceType, ctx, SubstitutionContext.Current);
83+
8284
foreach (var handler in _options.MockHandlers)
8385
{
84-
handler.OnMockCreated(instance, typedService.ServiceType, ctx, SubstitutionContext.Current);
86+
handler.OnMockCreated(mockCreatedContext);
8587
}
8688

8789
return instance;

AutofacContrib.NSubstitute/SubstituteForBuilderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public static SubstituteForBuilder<T> InjectProperties<T>(this SubstituteForBuil
2626
=> builder.ConfigureSubstitute((t, ctx) =>
2727
{
2828
ctx.InjectUnsetProperties(t);
29-
AutoPropertyInjectorMockHandler.Instance.OnMockCreated(t, typeof(T), ctx, builder.Context);
29+
var mockCtx = new MockCreatedContext(t, typeof(T), ctx, builder.Context);
30+
AutoPropertyInjectorMockHandler.Instance.OnMockCreated(mockCtx);
3031
});
3132
}
3233
}

0 commit comments

Comments
 (0)