public void GeneratesConstructor_Ok() { // Arrange var type = module.DefineType("ConstructorType", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public); var builder = new ProbingConstructorBuilder(null); var lib = Substitute.For<ILibrary>(); var probe = Substitute.For<IMethodCallProbe<IFoo>>(); var f = type.DefineField("_Foo", typeof(Func<string>), FieldAttributes.Private | FieldAttributes.InitOnly); // Act var ctor = builder.GenerateConstructor(type, typeof(IFoo), new MethodInfo[0], new[] { f }); // Assert var result = Activator.CreateInstance(type.CreateType(), lib, probe); Assert.IsNotNull(result); var probeField = result.GetType().GetField("$probe", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(probeField); Assert.AreEqual(probe, probeField.GetValue(result)); }
public void GeneratesConstructor_Ok() { // Arrange var type = module.DefineType("ConstructorType", TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AutoLayout | TypeAttributes.Public); var builder = new ProbingConstructorBuilder(null); var lib = Substitute.For <ILibrary>(); var probe = Substitute.For <IMethodCallProbe <IFoo> >(); var f = type.DefineField("_Foo", typeof(Func <string>), FieldAttributes.Private | FieldAttributes.InitOnly); // Act var ctor = builder.GenerateConstructor(type, typeof(IFoo), new MethodInfo[0], new[] { f }); // Assert var result = Activator.CreateInstance(type.CreateType(), lib, probe); Assert.IsNotNull(result); var probeField = result.GetType().GetField("$probe", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(probeField); Assert.AreEqual(probe, probeField.GetValue(result)); }