/
Automocker.cs
73 lines (68 loc) · 2.89 KB
/
Automocker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TypeMock.ArrangeActAssert;
namespace yads.Automocker
{
public class Automocker
{
private readonly IDictionary<Type, object> _typeRegistry = new Dictionary<Type, object>();
private static readonly MethodInfo TypeMockInstance;
static Automocker()
{
TypeMockInstance = Isolate.Fake.GetType()
.GetMethods()
.Where(m => m.Name == "Instance")
.Select(m => new
{
Method = m,
Params = m.GetParameters(),
Args = m.GetGenericArguments()
})
.Where(x => x.Params.Length == 0
&& x.Args.Length == 1)
.Select(x => x.Method)
.First();
}
public T CreateSut<T>()
{
var type = typeof (T);
var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
ConstructorInfo largestConstructor = null;
foreach (var constructor in constructors)
{
if (largestConstructor == null ||
largestConstructor.GetParameters().Length < constructor.GetParameters().Length)
{
largestConstructor = constructor;
}
}
var parameters = new List<object>();
foreach (var parameterInfo in largestConstructor.GetParameters())
{
if (!_typeRegistry.ContainsKey(parameterInfo.ParameterType))
{
_typeRegistry[parameterInfo.ParameterType] = GetMockInstance(parameterInfo.ParameterType);
}
parameters.Add(_typeRegistry[parameterInfo.ParameterType]);
}
return (T) largestConstructor.Invoke(parameters.ToArray());
}
public T GetMock<T>()
{
var type = typeof (T);
if (_typeRegistry.ContainsKey(type))
{
return (T) _typeRegistry[type];
}
var mockInstance = (T) GetMockInstance(type);
_typeRegistry[type] = mockInstance;
return mockInstance;
}
private static object GetMockInstance(Type type)
{
return TypeMockInstance.MakeGenericMethod(type).Invoke(Isolate.Fake, null);
}
}
}