main
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using Rhino.Mocks;
6
7namespace specs
8{
9 static class Create
10 {
11 static public T dependency<T>() where T : class
12 {
13 return An<T>();
14 }
15
16 static public Stub an<Stub>() where Stub : class
17 {
18 return An<Stub>();
19 }
20
21 static ItemToStub An<ItemToStub>() where ItemToStub : class
22 {
23 var type = typeof (ItemToStub);
24 if (type.IsClass)
25 {
26 var constructors =
27 type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
28 if (constructors.Any(x => x.GetParameters().Length == 0))
29 return MockRepository.GenerateMock<ItemToStub>();
30
31 return
32 MockRepository.GenerateMock<ItemToStub>(
33 get_parameters_for(get_greediest_constructor_from(constructors)).ToArray());
34 }
35 return MockRepository.GenerateMock<ItemToStub>();
36 }
37
38 static IEnumerable<object> get_parameters_for(ConstructorInfo constructor)
39 {
40 return constructor
41 .GetParameters()
42 .Select(x =>
43 {
44 if (x.ParameterType.IsValueType)
45 return Activator.CreateInstance(x.ParameterType);
46 if (x.ParameterType.IsSealed) return null;
47 return MockRepository.GenerateStub(x.ParameterType);
48 });
49 }
50
51 static ConstructorInfo get_greediest_constructor_from(IEnumerable<ConstructorInfo> constructors)
52 {
53 return constructors.OrderBy(x => x.GetParameters().Count()).Last();
54 }
55 }
56}