Beispiel #1
0
            public void NullTemplate_ShouldThrow()
            {
                // arrange
                var sut = new DynamicBuilder <TestClass>();

                // act
                Action overwriteAll = () => sut.OverwriteWithDelegate(null);

                // assert
                overwriteAll.Should().Throw <ArgumentNullException>();
            }
Beispiel #2
0
            public void ShouldCallPropertySetterForAllExpectedProperties()
            {
                // arrange
                var propertySetterMock = new Mock <IPropertySetter>();
                var sut = new DynamicBuilder <TestClass>(
                    propertySetter: propertySetterMock.Object);

                int expectedValue      = 1;
                var expectedProperties = new[]
                {
                    nameof(TestClass.PublicGetPublicSet),
                    nameof(TestClass.PublicGetInternalSet),
                    nameof(TestClass.PublicGetProtectedSet),
                    nameof(TestClass.PublicGetPrivateSet),
                    nameof(TestClass.PublicReadOnlyAutoProperty),
                    nameof(TestClass.PublicGetOfPrivateField),
                    nameof(TestClass.InternalGetInternalSet),
                    nameof(TestClass.InternalGetPrivateSet),
                    nameof(TestClass.ProtectedInternalGetProtectedInternalSet),
                    nameof(TestClass.ProtectedInternalGetInternalSet),
                    nameof(TestClass.ProtectedInternalGetPrivateSet)
                }
                .Select(propName => typeof(TestClass).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));

                // act
                sut.OverwriteWithDelegate(ctx =>
                {
                    if (ctx.PropertyType == typeof(int))
                    {
                        ctx.SetValue(expectedValue);
                    }
                });
                sut.Build();

                // assert
                foreach (var expectedProperty in expectedProperties)
                {
                    propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), expectedProperty, expectedValue), Times.Once());
                }
                propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), It.IsAny <PropertyInfo>(), It.IsAny <object>()), Times.Exactly(expectedProperties.Count()));
            }