Пример #1
0
        public void NestedPropertyValidator_MayBeConditional()
        {
            var mockModule = new Mock <IModule>(MockBehavior.Loose);

            IRequestContext context = null;

            var mockInjector = new Mock <IInjector>(MockBehavior.Strict);

            mockInjector
            .Setup(i => i.TryGet(typeof(IRequestContext), null))
            .Returns <Type, string>((iface, name) => context);

            Type proxyType = ProxyGenerator <IModule, ParameterValidator <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, mockInjector.Object, false);

            Assert.Throws <ValidationException>(() => module.DoSomething(new MyParameter2 {
                Value3 = new object(), Value2 = new MyParameter1 {
                    Value1 = null
                }
            }));

            context = new Mock <IRequestContext>(MockBehavior.Strict).Object;

            Assert.DoesNotThrow(() => module.DoSomething(new MyParameter2 {
                Value3 = new object(), Value2 = new MyParameter1 {
                    Value1 = null
                }
            }));
        }
Пример #2
0
        public void PropertyValidationTest()
        {
            var mockModule = new Mock <IModule>(MockBehavior.Loose);

            var mockInjector = new Mock <IInjector>(MockBehavior.Strict);

            mockInjector
            .Setup(i => i.TryGet(typeof(IRequestContext), null))
            .Returns <Type, string>((iface, name) => null);

            Type proxyType = ProxyGenerator <IModule, ParameterValidator <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, mockInjector.Object) !;

            Assert.DoesNotThrow(() => module.DoSomething(new MyParameter2
            {
                Value2 = new MyParameter1
                {
                    Value1 = new object()
                },
                Value3 = new object()
            }));

            var ex = Assert.Throws <ValidationException>(() => module.DoSomething(new MyParameter2
            {
                Value2 = null
            }));

            Assert.That(ex.TargetName, Is.EqualTo(nameof(MyParameter2.Value2)));

            ex = Assert.Throws <ValidationException>(() => module.DoSomething(new MyParameter2
            {
                Value2 = new MyParameter1
                {
                    Value1 = null
                },
                Value3 = new object()
            }));
            Assert.That(ex.TargetName, Is.EqualTo(nameof(MyParameter1.Value1)));

            ex = Assert.Throws <ValidationException>(() => module.DoSomething(new MyParameter2
            {
                Value2 = new MyParameter1
                {
                    Value1 = new object()
                },
                Value3 = null
            }));
            Assert.That(ex.TargetName, Is.EqualTo(nameof(MyParameter2.Value3)));
        }
Пример #3
0
        public void ParameterValidationTest()
        {
            var mockModule   = new Mock <IModule>(MockBehavior.Loose);
            var mockInjector = new Mock <IInjector>(MockBehavior.Strict);

            Type proxyType = ProxyGenerator <IModule, ParameterValidator <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, mockInjector.Object, false) !;

            Assert.DoesNotThrow(() => module.DoSomething("cica", 1));
            var ex = Assert.Throws <ValidationException>(() => module.DoSomething(null, null));

            Assert.That(ex.TargetName, Is.EqualTo("arg1"));
            Assert.That(ex.Message, Is.EqualTo(Errors.NULL_PARAM));
        }
Пример #4
0
        public void TransactionManager_ShouldCallCommitOnSuccessfulInvocation()
        {
            var mockTransaction = new Mock <IDbTransaction>(MockBehavior.Strict);

            mockTransaction.Setup(tr => tr.Commit());
            mockTransaction.Setup(tr => tr.Dispose());

            var mockDbConnection = new Mock <IDbConnection>(MockBehavior.Strict);

            mockDbConnection
            .Setup(conn => conn.BeginTransaction(IsolationLevel.Unspecified))
            .Returns(mockTransaction.Object);

            var mockModule = new Mock <IModule>(MockBehavior.Strict);

            mockModule.Setup(m => m.DoSomething(It.IsAny <object>()));

            Type proxyType = ProxyGenerator <IModule, TransactionManager <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, new Lazy <IDbConnection>(() => mockDbConnection.Object)) !;

            module.DoSomething(new object());

            mockDbConnection.Verify(conn => conn.BeginTransaction(IsolationLevel.Unspecified), Times.Once);
            mockTransaction.Verify(tr => tr.Commit(), Times.Once);
        }
Пример #5
0
        public void AggregatedParameterValidationTest()
        {
            var mockModule   = new Mock <IModule>(MockBehavior.Loose);
            var mockInjector = new Mock <IInjector>(MockBehavior.Strict);

            Type proxyType = ProxyGenerator <IModule, ParameterValidator <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, mockInjector.Object, true) !;

            Assert.DoesNotThrow(() => module.DoSomething("cica", 1));
            AggregateException ex = Assert.Throws <AggregateException>(() => module.DoSomething("kutya", null));

            Assert.That(ex.InnerExceptions.Count, Is.EqualTo(2));
            Assert.That(ex.InnerExceptions[0], Is.InstanceOf <ValidationException>());
            Assert.That(((ValidationException)ex.InnerExceptions[0]).TargetName, Is.EqualTo("arg1"));
            Assert.That(((ValidationException)ex.InnerExceptions[0]).Message, Is.EqualTo("ooops"));
            Assert.That(ex.InnerExceptions[1], Is.InstanceOf <ValidationException>());
            Assert.That(((ValidationException)ex.InnerExceptions[1]).TargetName, Is.EqualTo("arg2"));
        }
Пример #6
0
        public void PropertyValidator_ShouldHandleNulls()
        {
            var mockModule   = new Mock <IModule>(MockBehavior.Loose);
            var mockInjector = new Mock <IInjector>(MockBehavior.Strict);

            Type proxyType = ProxyGenerator <IModule, ParameterValidator <IModule> > .GetGeneratedType();

            IModule module = (IModule)Activator.CreateInstance(proxyType, mockModule.Object, mockInjector.Object) !;

            Assert.DoesNotThrow(() => module.DoSomething(null));
        }