예제 #1
0
        public void CreateWillNotTrackFilteredRequest()
        {
            // Fixture setup
            object tracked         = null;
            object requestedObject = new object();
            var    sut             = new DelegatingTracingBuilder();

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(requestedObject, dummyContainer);
            // Verify outcome
            Assert.Null(tracked);
            // Teardown
        }
        public void ExecuteDoesNotAssignFieldWhenSpecificationIsNotSatisfied()
        {
            // Fixture setup
            var spec = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            var sut       = new AutoPropertiesCommand <FieldHolder <object> >(spec);
            var specimen  = new FieldHolder <object>();
            var container = new DelegatingSpecimenContext {
                OnResolve = r => new object()
            };

            // Exercise system
            sut.Execute((object)specimen, container);
            // Verify outcome
            Assert.Null(specimen.Field);
            // Teardown
        }
        public void ExecuteWillQuerySpecificationWithCorrectFieldInfo()
        {
            // Fixture setup
            var expectedFieldInfo = typeof(FieldHolder <string>).GetField("Field");
            var verified          = false;
            var specMock          = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => verified = expectedFieldInfo.Equals(r)
            };
            var sut = new AutoPropertiesCommand <FieldHolder <string> >(specMock);
            // Exercise system
            var specimen       = new FieldHolder <string>();
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Execute((object)specimen, dummyContainer);
            // Verify outcome
            Assert.True(verified, "Mock verified");
            // Teardown
        }
예제 #4
0
        public void CreateFromMatchingRequestReturnsCorrectResult(
            Type from,
            Type to)
        {
            // Fixture setup
            var sut      = new TypeRelay(from, to);
            var expected = new object();
            var context  = new DelegatingSpecimenContext
            {
                OnResolve = r => to.Equals(r) ? expected : new object()
            };
            // Exercise system
            var actual = sut.Create(from, context);

            // Verify outcome
            Assert.Equal(expected, actual);
            // Teardown
        }
예제 #5
0
        public void ExecuteSetsCorrectFieldOnSpecimenWhenAnonymousValueIsImplied()
        {
            // Arrange
            var expectedValue   = new object();
            var expectedRequest = typeof(FieldHolder <object>).GetField("Field");
            var context         = new DelegatingSpecimenContext
            {
                OnResolve = r => expectedRequest.Equals(r) ? expectedValue : new NoSpecimen()
            };

            var sut      = new BindingCommand <FieldHolder <object>, object>(ph => ph.Field);
            var specimen = new FieldHolder <object>();

            // Act
            sut.Execute((object)specimen, context);
            // Assert
            Assert.Equal(expectedValue, specimen.Field);
        }
예제 #6
0
        public void CreateConvertsEnumerableToCorrectGenericType()
        {
            // Arrange
            var request         = typeof(IEnumerable <int>);
            var expectedRequest = new MultipleRequest(typeof(int));
            var enumerable      = Enumerable.Range(1, 3).Cast <object>();
            var context         = new DelegatingSpecimenContext {
                OnResolve = r => expectedRequest.Equals(r) ? (object)enumerable : new NoSpecimen()
            };

            var sut = new EnumerableRelay();
            // Act
            var result = sut.Create(request, context);
            // Assert
            var e = Assert.IsAssignableFrom <IEnumerable <int> >(result);

            Assert.True(enumerable.Cast <int>().SequenceEqual(e));
        }
예제 #7
0
        public void CreateWithReturnValueParameterlessDelegateRequestReturnsDelegateReturningValueSpecimen()
        {
            // Fixture setup
            var delegateRequest = typeof(Func <int>);
            var expectedResult  = 3;
            var sut             = new DelegateGenerator();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext
            {
                OnResolve = arg => expectedResult
            };
            var result = sut.Create(delegateRequest, dummyContainer);
            // Verify outcome
            var actualResult = ((Func <int>)result).Invoke();

            Assert.Equal(expectedResult, actualResult);
            // Teardown
        }
예제 #8
0
        public void DecoratedDisposableResultIsOnlyAddedToDisposablesOnce()
        {
            // Fixture setup
            var disposable = new DisposableSpy();
            var builder    = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => disposable
            };
            var sut = new DisposableTracker(builder);
            // Exercise system
            var dummyRequest = new object();
            var dummyContext = new DelegatingSpecimenContext();

            sut.Create(dummyRequest, dummyContext);
            sut.Create(dummyRequest, dummyContext);
            // Verify outcome
            Assert.Equal(1, sut.Disposables.Count(d => d == disposable));
            // Teardown
        }
        public void NonTypedUsesExplicitlySpecifiedTypeForFieldsAndPropertiesResolve()
        {
            // Arrange
            var sut = new AutoPropertiesCommand(typeof(object));

            var dummyPropertyValue = new object();
            var context            = new DelegatingSpecimenContext {
                OnResolve = r => dummyPropertyValue
            };

            var specimen = new PropertyHolder <object>();

            // Act
            sut.Execute(specimen, context);

            // Assert
            Assert.NotEqual(dummyPropertyValue, specimen.Property);
        }
예제 #10
0
        public void CreateReturnsCorrectResultWhenSpecificationIsNotSatisfied()
        {
            // Fixture setup
            var spec = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            var dummyBuilder = new DelegatingSpecimenBuilder();
            var sut          = new FilteringSpecimenBuilder(dummyBuilder, spec);
            var request      = new object();
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(request, dummyContainer);
            // Verify outcome
            var expectedResult = new NoSpecimen();

            Assert.Equal(expectedResult, result);
            // Teardown
        }
        public void SpecificationReceivesCorrectRequest()
        {
            // Arrange
            var expectedRequest = new object();
            var verified        = false;
            var specMock        = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => verified = expectedRequest == r
            };

            var dummyBuilder = new DelegatingSpecimenBuilder();
            var sut          = new FilteringSpecimenBuilder(dummyBuilder, specMock);
            // Act
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(expectedRequest, dummyContainer);
            // Assert
            Assert.True(verified, "Mock verified");
        }
        public void CreateFromMultipleSeededRequestWithNonTypeRequestReturnsCorrectResult(
            object innerRequest)
        {
            // Fixture setup
            var sut     = new MultipleToEnumerableRelay();
            var request = new MultipleRequest(
                new SeededRequest(
                    innerRequest,
                    new object()));
            // Exercise system
            var dummyContext = new DelegatingSpecimenContext();
            var actual       = sut.Create(request, dummyContext);
            // Verify outcome
            var expected = new NoSpecimen();

            Assert.Equal(expected, actual);
            // Teardown
        }
예제 #13
0
        public void CreateWithCorrectSeedWillReturnCorrectResult()
        {
            // Fixture setup
            var seed           = 7m;
            var seededRequest  = new SeededRequest(typeof(decimal), seed);
            var expectedResult = 3m;

            Func <decimal, decimal> factoryStub = s => s == seed ? expectedResult : 0m;

            var sut = new SeededFactory <decimal>(factoryStub);
            // Exercise system
            var dummyContext = new DelegatingSpecimenContext();
            var result       = sut.Create(seededRequest, dummyContext);

            // Verify outcome
            Assert.Equal(expectedResult, result);
            // Teardown
        }
예제 #14
0
        public void CreateWillPassThroughToDecoratedBuilder()
        {
            // Fixture setup
            object expectedSpecimen = Guid.NewGuid();
            var    decoratedBuilder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r
            };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            // Exercise system
            var    dummyContainer = new DelegatingSpecimenContext();
            object result         = sut.Create(expectedSpecimen, dummyContainer);

            // Verify outcome
            Assert.Equal(expectedSpecimen, result);

            // Teardown
        }
예제 #15
0
        public void ExceptionWithCorrectMessageIsThrownIfCreationFails()
        {
            // Fixture setup
            var request = new object();
            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw new Exception()
            };

            var sut     = new TerminatingWithPathSpecimenBuilder(builder);
            var context = new DelegatingSpecimenContext();

            // Exercise system and verify outcome
            var actualException = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));

            Assert.Contains("failed with exception", actualException.Message);
            // Teardown
        }
        public void ExecuteWillAssignCorrectPropertyValue()
        {
            // Fixture setup
            var expectedPropertyValue = new object();
            var container             = new DelegatingSpecimenContext {
                OnResolve = r => expectedPropertyValue
            };

            var sut = new AutoPropertiesCommand <PropertyHolder <object> >();

            var specimen = new PropertyHolder <object>();

            // Exercise system
            sut.Execute((object)specimen, container);
            // Verify outcome
            Assert.Equal(expectedPropertyValue, specimen.Property);
            // Teardown
        }
예제 #17
0
        public void CreateWillTrackCreatedSpecimen()
        {
            // Fixture setup
            object tracked         = null;
            object createdSpecimen = Guid.NewGuid();
            var    container       = new DelegatingSpecimenContext {
                OnResolve = r => createdSpecimen
            };
            var sut = new DelegatingTracingBuilder();

            // Exercise system
            object res = sut.Create(new object(), container);

            // Verify outcome
            Assert.Equal(res, tracked);

            // Teardown
        }
        public void InnerBuilderExceptionIsWrappedByCreationException()
        {
            // Arrange
            var request   = new object();
            var exception = new Exception();
            var builder   = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw exception
            };

            var sut     = new TerminatingWithPathSpecimenBuilder(builder);
            var context = new DelegatingSpecimenContext();

            // Act & assert
            var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));

            Assert.Same(exception, actualEx.InnerException);
        }
        public void CreateWillReturnCorrectResult()
        {
            // Arrange
            var expectedSpecimen = new object();
            var stubBuilder      = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => expectedSpecimen
            };

            var sut = new TerminatingWithPathSpecimenBuilder(stubBuilder);
            // Act
            var dummyRequest   = new object();
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(dummyRequest, dummyContainer);

            // Assert
            Assert.Equal(expectedSpecimen, result);
        }
예제 #20
0
        public void SpecimenFormatterIsGivenCorrectTextWriter()
        {
            // Fixture setup
            var expectedWriter = new StringWriter();
            var sut            = new TraceWriter(expectedWriter, new DelegatingTracingBuilder());

            bool verified = false;

            sut.TraceSpecimenFormatter = (tw, r, i) => verified = tw == expectedWriter;
            // Exercise system
            var dummyRequest   = new object();
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(dummyRequest, dummyContainer);
            // Verify outcome
            Assert.True(verified);
            // Teardown
        }
예제 #21
0
        public void CreateInvokesDecoratedBuilderWithCorrectParametersOnSutWithDoubleAction()
        {
            // Arrange
            var expectedRequest   = new object();
            var expectedContainer = new DelegatingSpecimenContext();

            var verified    = false;
            var builderMock = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => verified = r == expectedRequest && c == expectedContainer
            };

            var dummyCommand = new DelegatingSpecimenCommand();
            var sut          = new Postprocessor <bool>(builderMock, dummyCommand);

            // Act
            sut.Create(expectedRequest, expectedContainer);
            // Assert
            Assert.True(verified, "Mock verified");
        }
        public void CreateWithNonEnumerableParameterRequestReturnsCorrectResult(
            Type argumentType)
        {
            var parameterInfo =
                typeof(SingleParameterType <>)
                .MakeGenericType(new[] { argumentType })
                .GetConstructors()
                .First()
                .GetParameters()
                .First();
            var sut = new OmitArrayParameterRequestRelay();

            var dummyContext = new DelegatingSpecimenContext();
            var actual       = sut.Create(parameterInfo, dummyContext);

            var expected = new NoSpecimen();

            Assert.Equal(expected, actual);
        }
예제 #23
0
        public void CreateWillInvokeDecoratedBuilderWithCorrectParameters()
        {
            // Arrange
            var expectedRequest   = new object();
            var expectedContainer = new DelegatingSpecimenContext();

            var verified    = false;
            var mockBuilder = new TracingBuilder(new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => verified = expectedRequest == r && expectedContainer == c
            });

            var dummyWriter = TextWriter.Null;
            var sut         = new TraceWriter(dummyWriter, mockBuilder);

            // Act
            sut.Create(expectedRequest, expectedContainer);
            // Assert
            Assert.True(verified, "Mock verified");
        }
예제 #24
0
        public void CreateReturnsResultFromDecoratedBuilder()
        {
            // Arrange
            var request        = new object();
            var ctx            = new DelegatingSpecimenContext();
            var expectedResult = new object();

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => (r == request) && (c == ctx) ? expectedResult : new NoSpecimen()
            };

            var sut = new DisposableTracker(builder);
            // Act
            var result = sut.Create(request, ctx);

            // Assert
            Assert.Equal(expectedResult, result);
        }
예제 #25
0
        public void DisposeRemovesAllDisposables()
        {
            // Arrange
            var builder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => new DisposableSpy()
            };
            var sut = new DisposableTracker(builder);

            var dummyRequest = new object();
            var dummyContext = new DelegatingSpecimenContext();

            sut.Create(dummyRequest, dummyContext);
            sut.Create(dummyRequest, dummyContext);
            sut.Create(dummyRequest, dummyContext);
            // Act
            sut.Dispose();
            // Assert
            Assert.Empty(sut.Disposables);
        }
예제 #26
0
        public void IgnoresRequestIfGenericTypeDoesNotMatchExactly()
        {
            // Arrange
            var from = typeof(IEnumerable <>);
            var to   = typeof(List <>);
            var sut  = new TypeRelay(from, to);

            var request      = typeof(int[]);
            var dummyContext = new DelegatingSpecimenContext
            {
                OnResolve = _ => new object()
            };

            // Act
            var result = sut.Create(request, dummyContext);

            // Assert
            Assert.IsType <NoSpecimen>(result);
        }
        public void ShouldWrapObjectCreationExceptionsFromInnerBuilders()
        {
            // Arrange
            var request          = new object();
            var exceptionToThrow = new ObjectCreationException("Creation failed.");

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw exceptionToThrow
            };

            var sut     = new TerminatingWithPathSpecimenBuilder(builder);
            var context = new DelegatingSpecimenContext();

            // Act & assert
            var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));

            Assert.NotEqual(exceptionToThrow, actualEx);
        }
        public void CreateWillInvokeDecoratedBuilderWithCorrectParameters()
        {
            // Arrange
            var expectedRequest   = new object();
            var expectedContainer = new DelegatingSpecimenContext();

            var verified    = false;
            var mockBuilder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => verified = expectedRequest == r && expectedContainer == c
            };

            var sut = new TerminatingWithPathSpecimenBuilder(mockBuilder);

            // Act
            sut.Create(expectedRequest, expectedContainer);
            // Assert
            Assert.True(verified, "Mock verified");
        }
예제 #29
0
        public void CreateWillReturnCorrectResult()
        {
            // Fixture setup
            var expectedSpecimen = new object();
            var stubBuilder      = new TracingBuilder(new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => expectedSpecimen
            });

            var dummyWriter = TextWriter.Null;
            var sut         = new TraceWriter(dummyWriter, stubBuilder);
            // Exercise system
            var dummyRequest   = new object();
            var dummyContainer = new DelegatingSpecimenContext();
            var result         = sut.Create(dummyRequest, dummyContainer);

            // Verify outcome
            Assert.Equal(expectedSpecimen, result);
            // Teardown
        }
예제 #30
0
        public void InnerBuilderExceptionIsWrappedByCreationException()
        {
            // Fixture setup
            var request   = new object();
            var exception = new Exception();
            var builder   = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw exception
            };

            var sut     = new TerminatingWithPathSpecimenBuilder(builder);
            var context = new DelegatingSpecimenContext();

            // Exercise system and verify outcome
            var actualEx = Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));

            Assert.Same(exception, actualEx.InnerException);
            // Teardown
        }