public void AddBinding_Throws_WhenPropertyExpressionIsNull()
        {
            var viewModel       = new TestViewModel();
            var serviceProvider = new Mock <IServiceProvider>();
            var wemf            = new Mock <IWeakEventManagerFactory>();
            var wem             = new Mock <IWeakEventManager>();
            var bindingFactory  = new Mock <IBindingFactory>();

            serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object)
            .Verifiable();
            serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object)
            .Verifiable();
            wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable();

            var component = new TestComponent(serviceProvider.Object);

            Should.Throw <BindingException>(() => component.AddBinding <TestViewModel, string>(viewModel, null));

            serviceProvider.Verify();
            wemf.Verify();

            serviceProvider.VerifyNoOtherCalls();
            wemf.VerifyNoOtherCalls();
            bindingFactory.VerifyNoOtherCalls();
            wem.VerifyNoOtherCalls();
        }
Beispiel #2
0
        public void AddBinding_Throws_WhenBindingMemberIsAMethod()
        {
            var viewModel       = new TestViewModel();
            var serviceProvider = new Mock <IServiceProvider>();
            var wemf            = new Mock <IWeakEventManagerFactory>();
            var wem             = new Mock <IWeakEventManager>();
            var bindingFactory  = new Mock <IBindingFactory>();
            var binding         = new Mock <IBinding>();

            serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object)
            .Verifiable();
            serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object)
            .Verifiable();
            wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable();
            bindingFactory.Setup(x => x.Create(It.IsAny <INotifyPropertyChanged>(), It.IsAny <PropertyInfo>(),
                                               It.IsAny <IWeakEventManager>())).Returns(binding.Object).Verifiable();

            var component = new TestComponent(serviceProvider.Object);

            Should.Throw <BindingException>(() => component.AddBinding(viewModel, x => x.ShouldRender()));

            serviceProvider.Verify();
            wemf.Verify();
            serviceProvider.VerifyNoOtherCalls();
            wemf.VerifyNoOtherCalls();
            bindingFactory.VerifyNoOtherCalls();
            binding.VerifyNoOtherCalls();
            wem.VerifyNoOtherCalls();
        }
        public void AddBinding_SkipsAddingBindingIfAlreadyExists()
        {
            var viewModel       = new TestViewModel();
            var serviceProvider = new Mock <IServiceProvider>();
            var wemf            = new Mock <IWeakEventManagerFactory>();
            var wem             = new Mock <IWeakEventManager>();
            var bindingFactory  = new Mock <IBindingFactory>();
            var binding         = new Mock <IBinding>();

            serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object)
            .Verifiable();
            serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object)
            .Verifiable();
            wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable();
            bindingFactory.Setup(x => x.Create(It.IsAny <INotifyPropertyChanged>(), It.IsAny <PropertyInfo>(),
                                               It.IsAny <IWeakEventManager>())).Returns(binding.Object).Verifiable();
            binding.Setup(x => x.GetValue()).Returns("Test").Verifiable();
            binding.Setup(x => x.Initialize()).Verifiable();
            wem.Setup(x => x.AddWeakEventListener(It.IsAny <IBinding>(), nameof(IBinding.BindingValueChanged),
                                                  It.IsAny <Action <IBinding, EventArgs> >())).Verifiable();

            var component = new TestComponent(serviceProvider.Object);
            var res       = component.AddBinding(viewModel, x => x.TestProperty);

            res.ShouldBe("Test");

            res = component.AddBinding(viewModel, x => x.TestProperty);
            res.ShouldBe("Test");

            serviceProvider.Verify();
            wemf.Verify();
            wem.Verify();
            bindingFactory.Verify();
            binding.Verify();
            binding.Verify();

            serviceProvider.VerifyNoOtherCalls();
            wemf.VerifyNoOtherCalls();
            bindingFactory.VerifyNoOtherCalls();
            binding.VerifyNoOtherCalls();
            wem.VerifyNoOtherCalls();
        }
        public void Binding_BindingValueChanged_GetsInvoked()
        {
            var viewModel       = new TestViewModel();
            var serviceProvider = new Mock <IServiceProvider>();
            var wemf            = new Mock <IWeakEventManagerFactory>();
            var wem             = new Mock <IWeakEventManager>();
            var bindingFactory  = new Mock <IBindingFactory>();
            var binding         = new Mock <IBinding>();

            serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object)
            .Verifiable();
            serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object)
            .Verifiable();
            wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable();
            wem.Setup(x =>
                      x.AddWeakEventListener(It.IsAny <IBinding>(), nameof(IBinding.BindingValueChanged),
                                             It.IsAny <Action <IBinding, EventArgs> >()))
            .Callback <IBinding, string, Action <IBinding, EventArgs> >((b, e, a) =>
                                                                        b.BindingValueChanged += (s, args) => a(b, args)).Verifiable();
            bindingFactory.Setup(x => x.Create(It.IsAny <INotifyPropertyChanged>(), It.IsAny <PropertyInfo>(),
                                               It.IsAny <IWeakEventManager>())).Returns(binding.Object).Verifiable();
            binding.Setup(x => x.GetValue()).Returns("Test").Verifiable();
            binding.Setup(x => x.Initialize()).Verifiable();
            binding.SetupAdd(m => m.BindingValueChanged += It.IsAny <EventHandler>()).Verifiable();


            var bindingChangedInvoked = false;
            var component             = new TestComponent(serviceProvider.Object)
            {
                BindingChangedAction = () => bindingChangedInvoked = true
            };
            var res = component.AddBinding(viewModel, x => x.TestProperty);

            binding.Raise(x => x.BindingValueChanged += null, EventArgs.Empty);
            res.ShouldBe("Test");
            bindingChangedInvoked.ShouldBeTrue();

            serviceProvider.Verify();
            wemf.Verify();
            bindingFactory.Verify();
            binding.Verify();
            wem.Verify();

            serviceProvider.VerifyNoOtherCalls();
            wemf.VerifyNoOtherCalls();
            bindingFactory.VerifyNoOtherCalls();
            binding.VerifyNoOtherCalls();
            wem.VerifyNoOtherCalls();
        }
        public async Task DisposeAsync_RemovesEventListenersForBindings()
        {
            var viewModel       = new TestViewModel();
            var serviceProvider = new Mock <IServiceProvider>();
            var wemf            = new Mock <IWeakEventManagerFactory>();
            var wem             = new Mock <IWeakEventManager>();
            var bindingFactory  = new Mock <IBindingFactory>();
            var binding         = new Mock <IBinding>();

            serviceProvider.Setup(x => x.GetService(typeof(IWeakEventManagerFactory))).Returns(wemf.Object)
            .Verifiable();
            serviceProvider.Setup(x => x.GetService(typeof(IBindingFactory))).Returns(bindingFactory.Object)
            .Verifiable();
            wemf.Setup(x => x.Create()).Returns(wem.Object).Verifiable();
            wem.Setup(x => x.AddWeakEventListener(It.IsAny <IBinding>(), nameof(IBinding.BindingValueChanged),
                                                  It.IsAny <Action <IBinding, EventArgs> >())).Verifiable();
            wem.Setup(x => x.RemoveWeakEventListener(It.IsAny <IBinding>())).Verifiable();
            bindingFactory.Setup(x => x.Create(It.IsAny <INotifyPropertyChanged>(), It.IsAny <PropertyInfo>(),
                                               It.IsAny <IWeakEventManager>())).Returns(binding.Object).Verifiable();
            binding.Setup(x => x.GetValue()).Returns("Test").Verifiable();
            binding.Setup(x => x.Initialize()).Verifiable();
            binding.Setup(x => x.Dispose()).Verifiable();

            var component = new TestComponent(serviceProvider.Object);

            component.AddBinding(viewModel, x => x.TestProperty);
            await component.DisposeAsync();

            serviceProvider.Verify();
            wemf.Verify();
            bindingFactory.Verify();
            binding.Verify();
            wem.Verify();

            serviceProvider.VerifyNoOtherCalls();
            wemf.VerifyNoOtherCalls();
            bindingFactory.VerifyNoOtherCalls();
            binding.VerifyNoOtherCalls();
            wem.VerifyNoOtherCalls();
        }