-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
AutoMoqer.cs
186 lines (152 loc) · 5.84 KB
/
AutoMoqer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using DryIoc;
using Moq;
using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo;
namespace NzbDrone.Test.Common.AutoMoq
{
[DebuggerStepThrough]
public class AutoMoqer
{
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
private IContainer _container;
private IDictionary<Type, object> _registeredMocks;
public AutoMoqer()
{
SetupAutoMoqer(CreateTestContainer(new Container()));
}
public IContainer Container => _container;
public virtual T Resolve<T>()
{
var result = _container.Resolve<T>();
SetConstant(result);
return result;
}
public virtual Mock<T> GetMock<T>()
where T : class
{
return GetMock<T>(DefaultBehavior);
}
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
where T : class
{
var type = GetTheMockType<T>();
if (GetMockHasNotBeenCalledForThisType(type))
{
CreateANewMockAndRegisterIt<T>(type, behavior);
}
var mock = TheRegisteredMockForThisType<T>(type);
if (behavior != MockBehavior.Default && mock.Behavior == MockBehavior.Default)
{
throw new InvalidOperationException("Unable to change be behaviour of a an existing mock.");
}
return mock;
}
public virtual void SetMock(Type type, Mock mock)
{
if (GetMockHasNotBeenCalledForThisType(type))
{
_registeredMocks.Add(type, mock);
}
if (mock != null)
{
_container.RegisterInstance(type, mock.Object, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
}
}
public virtual void SetConstant<T>(T instance)
{
_container.RegisterInstance(instance, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
SetMock(instance.GetType(), null);
}
private IContainer CreateTestContainer(IContainer container)
{
var c = container.CreateChild(IfAlreadyRegistered.Replace,
container.Rules
.WithDynamicRegistration((serviceType, serviceKey) =>
{
// ignore services with non-default key
if (serviceKey != null)
{
return null;
}
if (serviceType == typeof(object))
{
return null;
}
if (serviceType.IsGenericType && serviceType.IsOpenGeneric())
{
return null;
}
if (serviceType == typeof(System.Text.Json.Serialization.JsonConverter))
{
return null;
}
// get the Mock object for the abstract class or interface
if (serviceType.IsInterface || serviceType.IsAbstract)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType);
var mockFactory = DelegateFactory.Of(r =>
{
var mock = (Mock)r.Resolve(mockType);
SetMock(serviceType, mock);
return mock.Object;
}, Reuse.Singleton);
return new[] { new DynamicRegistration(mockFactory, IfAlreadyRegistered.Keep) };
}
// concrete types
var concreteTypeFactory = serviceType.ToFactory(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic);
return new[] { new DynamicRegistration(concreteTypeFactory) };
},
DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback));
c.Register(typeof(Mock<>), Reuse.Singleton, FactoryMethod.DefaultConstructor());
return c;
}
private void SetupAutoMoqer(IContainer container)
{
_container = container;
container.RegisterInstance(this);
_registeredMocks = new Dictionary<Type, object>();
LoadPlatformLibrary();
AssemblyLoader.RegisterNativeResolver(new[] { "System.Data.SQLite", "Radarr.Core" });
}
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
where T : class
{
return (Mock<T>)_registeredMocks.First(x => x.Key == type).Value;
}
private void CreateANewMockAndRegisterIt<T>(Type type, MockBehavior behavior)
where T : class
{
var mock = new Mock<T>(behavior);
_container.RegisterInstance(mock.Object);
SetMock(type, mock);
}
private bool GetMockHasNotBeenCalledForThisType(Type type)
{
return !_registeredMocks.ContainsKey(type);
}
private static Type GetTheMockType<T>()
where T : class
{
return typeof(T);
}
private void LoadPlatformLibrary()
{
var assemblyName = "Radarr.Windows";
if (OsInfo.IsNotWindows)
{
assemblyName = "Radarr.Mono";
}
if (!File.Exists(assemblyName + ".dll"))
{
return;
}
Assembly.Load(assemblyName);
}
}
}