public void BuildRuntimeType_ReadonlyProperties_Override()
        {
            // Arrange
            string value = "test value";

            Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict);

            managerMock.Setup(x => x.GetValue(nameof(IInterfaceWithReadonlyProperty.ReadonlyStringProperty)))
            .Returns(value);

            var        manager   = managerMock.Object;
            MethodInfo getMethod = manager.GetType().GetMethod("GetValue");
            MethodInfo setMethod = manager.GetType().GetMethod("SetValue");

            // Act
            Stopwatch sw          = Stopwatch.StartNew();
            Type      runtimeType = RuntimeProxyBuilder.BuildRuntimeType(typeof(IInterfaceWithReadonlyProperty), getMethod, setMethod);
            IInterfaceWithReadonlyProperty impl = (IInterfaceWithReadonlyProperty)Activator.CreateInstance(runtimeType, manager);
            string returnedValue = impl.ReadonlyStringProperty;

            sw.Stop();
            Trace.WriteLine(sw.ElapsedMilliseconds);

            // Assert
            Assert.AreEqual(value, returnedValue);
        }
Beispiel #2
0
        public void Implementation_InterfaceWithReadonlyProperty_NotifyPropertyAccessed()
        {
            // Arrange
            Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>();

            observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>());
            observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>());
            observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true);

            Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>();

            // Act
            var manager = new RuntimeProxyManager <IInterfaceWithReadonlyProperty>(observableMock.Object);

            manager.Subscribe(subscriberMock.Object);
            IInterfaceWithReadonlyProperty impl = manager.Implementation;

            Trace.WriteLine(impl.ReadonlyStringProperty);

            // Assert
            subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(impl.ReadonlyStringProperty))), Times.Once);
        }