public static IArrangeDependencies Config <TInterface, TImplementation>(Action <IArrangeBuilder <ContainerBuilder> > config = null) where TInterface : class where TImplementation : class, TInterface { var arrangeBuilder = new ArrangeBuilder(); config?.Invoke(arrangeBuilder); arrangeBuilder.UseImplementation <TInterface, TImplementation>(); var containerBuilder = arrangeBuilder.Build(); containerBuilder.Register((context) => { var constructors = typeof(TImplementation).GetConstructors(); var parameters = constructors.SelectMany(x => x.GetParameters()).Where(x => x.ParameterType.IsInterface).Distinct(); var args = new List <object>(); foreach (var parameter in parameters) { args.Add(context.ResolveOptional(parameter.ParameterType) ?? MockHelper.CreateMock(parameter.ParameterType)); } return(Activator.CreateInstance(typeof(TImplementation), args.ToArray()) as TImplementation); }).As <TInterface>().IfNotRegistered(typeof(TInterface)); var container = containerBuilder.Build(); return(new ArrangeDependencies(container)); }
public void Formatters_Ctor_Sets_Direct_Access_Property(Type mockType, string formatterPropertyName) { var mockFormatter = MockHelper.CreateMock(mockType); var mockTypeFormatter = (ITypeFormatter)mockFormatter.Object; var resolver = new TypeFormatterResolver(mockTypeFormatter); var formatterProperty = resolver .GetType() .GetProperty(formatterPropertyName); var formatterValue = formatterProperty.GetValue(resolver, null); Assert.AreSame(mockTypeFormatter, formatterValue); }
private static void AddDependency <TInterface>(ArrangeBuilder arrangeBuilder, Type implementation) where TInterface : class { arrangeBuilder.AddDependency((containerBuilder) => containerBuilder.Register((context) => { var constructors = implementation.GetConstructors(); var parameters = constructors.SelectMany(x => x.GetParameters()).Distinct(); var args = new List <object>(); foreach (var parameter in parameters) { args.Add(context.ResolveOptional(parameter.ParameterType) ?? MockHelper.CreateMock(parameter.ParameterType)); } return(Activator.CreateInstance(implementation, args.ToArray()) as TInterface); }).As <TInterface>()); }
public void FormatType_With_Resolver_Uses_Resolver_When_Supplied(Type valueType, string formatterMethodName, object rawValue) { Mock mockFormatter = null; void createMock <T>(T expectedValue) { var mock = MockHelper.CreateMock <ITypeFormatter <T> >(); mock.Setup(r => r.Format(expectedValue)).Returns("!!!"); mockFormatter = mock; } var formatterMethod = typeof(Formatter) .GetMethods(BindingFlags.Static | BindingFlags.Public) .Single(r => r.Name == formatterMethodName && r.GetParameters().Length == 2); Func <object, TypeFormatterResolver, string> callFormatter; callFormatter = (t, r) => formatterMethod.Invoke(null, new object[] { t, r }) as string; var value = DataRowParser.ConvertExpected(valueType, rawValue); if (valueType == typeof(bool)) { createMock <bool>((bool)value); } else if (valueType == typeof(byte)) { createMock <byte>((byte)value); } else if (valueType == typeof(char)) { createMock <char>((char)value); } else if (valueType == typeof(Int16)) { createMock <short>((short)value); } else if (valueType == typeof(UInt16)) { createMock <ushort>((ushort)value); } else if (valueType == typeof(Int32)) { createMock <int>((int)value); } else if (valueType == typeof(UInt32)) { createMock <uint>((uint)value); } else if (valueType == typeof(Int64)) { createMock <long>((long)value); } else if (valueType == typeof(UInt64)) { createMock <ulong>((ulong)value); } else if (valueType == typeof(float)) { createMock <float>((float)value); } else if (valueType == typeof(double)) { createMock <double>((double)value); } else if (valueType == typeof(decimal)) { createMock <decimal>((decimal)value); } else if (valueType == typeof(DateTime)) { createMock <DateTime>((DateTime)value); } else if (valueType == typeof(DateTimeOffset)) { createMock <DateTimeOffset>((DateTimeOffset)value); } else if (valueType == typeof(Guid)) { createMock <Guid>((Guid)value); } else if (valueType == typeof(byte[])) { createMock <byte[]>((byte[])value); } else if (valueType == typeof(string)) { createMock <string>((string)value); } else { throw new NotImplementedException(); } // Null type resolver should call normal formatter Assert.AreNotEqual("!!!", callFormatter(value, null)); // Type resolver with no formatter for type should call normal formatter var emptyTypeResolver = new TypeFormatterResolver(); Assert.AreNotEqual("!!!", callFormatter(value, emptyTypeResolver)); // If resolver contains formatter for type then it should be used var typeResolver = new TypeFormatterResolver((ITypeFormatter)mockFormatter.Object); Assert.AreEqual("!!!", callFormatter(value, typeResolver)); }
public void Serialise_Uses_Formatter_For_Dates_And_Byte_Arrays_But_Not_Json_Spec_Types(Type valueType, bool mustUseFormatter, string propertyName, object originalValueRaw) { Mock mockFormatter = null; void createMock <T>(T expectedValue) { var mock = MockHelper.CreateMock <ITypeFormatter <T> >(); mock.Setup(r => r.Format(expectedValue)).Returns("!!!"); mockFormatter = mock; } var expectedNormal = DataRowParser.ConvertExpected(valueType, originalValueRaw); var valueProperty = typeof(ValueTypes).GetProperty(propertyName); if (valueType == typeof(bool)) { createMock <bool>((bool)expectedNormal); } else if (valueType == typeof(byte)) { createMock <byte>((byte)expectedNormal); } else if (valueType == typeof(char)) { createMock <char>((char)expectedNormal); } else if (valueType == typeof(Int16)) { createMock <short>((short)expectedNormal); } else if (valueType == typeof(UInt16)) { createMock <ushort>((ushort)expectedNormal); } else if (valueType == typeof(Int32)) { createMock <int>((int)expectedNormal); } else if (valueType == typeof(UInt32)) { createMock <uint>((uint)expectedNormal); } else if (valueType == typeof(Int64)) { createMock <long>((long)expectedNormal); } else if (valueType == typeof(UInt64)) { createMock <ulong>((ulong)expectedNormal); } else if (valueType == typeof(float)) { createMock <float>((float)expectedNormal); } else if (valueType == typeof(double)) { createMock <double>((double)expectedNormal); } else if (valueType == typeof(decimal)) { createMock <decimal>((decimal)expectedNormal); } else if (valueType == typeof(DateTime)) { createMock <DateTime>((DateTime)expectedNormal); } else if (valueType == typeof(DateTimeOffset)) { createMock <DateTimeOffset>((DateTimeOffset)expectedNormal); } else if (valueType == typeof(Guid)) { createMock <Guid>((Guid)expectedNormal); } else if (valueType == typeof(string)) { createMock <string>((string)expectedNormal); } else if (valueType == typeof(byte[])) { createMock <byte[]>((byte[])expectedNormal); } else { throw new NotImplementedException(); } var typeResolver = new TypeFormatterResolver((ITypeFormatter)mockFormatter.Object); var originalInstance = new ValueTypes(); valueProperty.SetValue(originalInstance, expectedNormal); var jsonText = _Serialiser.Serialise(originalInstance, typeResolver); var jObject = JObject.Parse(jsonText); var jProperty = jObject.Property(propertyName); if (mustUseFormatter) { Assert.AreEqual("!!!", jProperty.Value.ToString()); } else { Assert.AreEqual(expectedNormal, jProperty.Value.ToObject(valueProperty.PropertyType)); } }