コード例 #1
0
        public void CreateOnMultipleThreadsConcurrentlyWorks()
        {
            // Fixture setup
            var tracer = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => 99
            });
            var sut          = new TerminatingWithPathSpecimenBuilder(tracer);
            var dummyContext = new DelegatingSpecimenContext()
            {
                OnResolve = (r) => 99
            };

            // Exercise system
            int[] specimens = Enumerable.Range(0, 1000)
                              .AsParallel()
                              .WithDegreeOfParallelism(8)
                              .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                              .Select(x => (int)sut.Create(typeof(int), dummyContext))
                              .ToArray();
            // Verify outcome
            Assert.Equal(1000, specimens.Length);
            Assert.True(specimens.All(s => s == 99));
            // Teardown
        }
コード例 #2
0
        public void SpecimenRequestsAreEmptyWhenAllThatWereRequestedAreCreated()
        {
            // Fixture setup
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : new object()
            };

            var sut = new TerminatingWithPathSpecimenBuilder(builder);
            // Cause sut to be executed recursively for multiple times.
            var context = new SpecimenContext(sut);

            // Exercise system
            sut.Create(firstRequest, context);

            // Verify outcome
            Assert.Empty(sut.SpecimenRequests);
            // Teardown
        }
コード例 #3
0
        public void NestedObjectCreationExceptionIsWrappedForOneTimeOnly()
        {
            // Arrange
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();

            var expectedInnerException = new ObjectCreationException();

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : throw expectedInnerException
            };

            var sut = new TerminatingWithPathSpecimenBuilder(builder);
            // Cause sut to be executed recursively for multiple times.
            var context = new SpecimenContext(sut);

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

            Assert.Equal(expectedInnerException, actualEx.InnerException);
        }
コード例 #4
0
        public void SpecimenRequestsRaisedFromTracerAreRecordedCorrectly()
        {
            // Arrange
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();

            object[] capturedRequests = null;
            TerminatingWithPathSpecimenBuilder sut = null;

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) =>
                {
                    if (requestQueue.Count > 0)
                    {
                        return(c.Resolve(requestQueue.Dequeue()));
                    }

                    capturedRequests = sut.SpecimenRequests.ToArray();
                    return(new object());
                }
            };

            sut = new TerminatingWithPathSpecimenBuilder(builder);
            // Cause sut to be executed recursively for multiple times.
            var context = new SpecimenContext(sut);

            // Act
            sut.Create(firstRequest, context);

            // Assert
            Assert.NotNull(capturedRequests);
            Assert.Equal(requests, capturedRequests);
        }
コード例 #5
0
        public void CreateThrowsWithAutoMockingHintOnInterfaceOrAbcRequest(
            object request,
            string requestType)
        {
            var tracer =
                new DelegatingTracingBuilder(
                    new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => new NoSpecimen(request)
            });
            var sut = new TerminatingWithPathSpecimenBuilder(tracer);

            var context = new SpecimenContext(sut);
            var e       = Assert.Throws <ObjectCreationException>(
                () => sut.Create(request, context));

            Assert.Contains(
                requestType,
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "auto-mocking",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
            Assert.Contains(
                "request path",
                e.Message,
                StringComparison.CurrentCultureIgnoreCase);
        }
コード例 #6
0
        public void CreateThrowsWhenNoSpecimenIsReturnedFromTheDecoratedGraph()
        {
            // Arrange
            var requests = new[] { new object(), new object(), new object() };
            var builder  = new DelegatingSpecimenBuilder
            {
                // Returns NoSpecimen only on the last specimen request
                OnCreate = (r, c) => r == requests[2] ? new NoSpecimen() : new object()
            };

            var sut       = new TerminatingWithPathSpecimenBuilder(builder);
            var container = new SpecimenContext(sut);

            // Act & assert
            Assert.Null(Record.Exception(() => sut.Create(requests[0], container)));
            Assert.Null(Record.Exception(() => sut.Create(requests[1], container)));
            Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(requests[2], container));
        }
コード例 #7
0
        public void CreateThrowsWhenNoSpecimenIsReturnedFromTheDecoratedGraph()
        {
            // Fixture setup
            var requests = new[] { new object(), new object(), new object() };
            var tracer   = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                // Returns NoSpecimen only on the last specimen request
                OnCreate = (r, c) => (r == requests[2]) ? new NoSpecimen() : new object(),
            });

            var sut       = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);

            // Exercise system and verify outcome
            Assert.DoesNotThrow(() => sut.Create(requests[0], container));
            Assert.DoesNotThrow(() => sut.Create(requests[1], container));
            Assert.Throws <ObjectCreationException>(() => sut.Create(requests[2], container));
            // Teardown
        }
コード例 #8
0
        public void ObjectCreationExceptionIsThrownIfCreationFails()
        {
            // Arrange
            var request = new object();
            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw new Exception()
            };

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

            // Act & assert
            Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));
        }
コード例 #9
0
        public void ObjectCreationExceptionIsThrownIfCreationFails()
        {
            // 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
            Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(request, context));
            // Teardown
        }
コード例 #10
0
        public void ExceptionWithCorrectMessageIsThrownIfCreationFails()
        {
            // Arrange
            var request = new object();
            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => throw new Exception()
            };

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

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

            Assert.Contains("failed with exception", actualException.Message);
        }
コード例 #11
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
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        public void CreateWillReturnCorrectResult()
        {
            // Fixture setup
            var expectedSpecimen = new object();
            var stubBuilder      = new TracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => expectedSpecimen
            });

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

            // Verify outcome
            Assert.Equal(expectedSpecimen, result);
            // Teardown
        }
コード例 #15
0
        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);
        }
コード例 #16
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
        }
コード例 #17
0
        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");
        }
コード例 #18
0
        public void CreateWillInvokeDecoratedBuilderWithCorrectParameters()
        {
            // Fixture setup
            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 sut = new TerminatingWithPathSpecimenBuilder(mockBuilder);

            // Exercise system
            sut.Create(expectedRequest, expectedContainer);
            // Verify outcome
            Assert.True(verified, "Mock verified");
            // Teardown
        }
コード例 #19
0
        public void ShouldWrapObjectCreationExceptionsFromInnerBuilders()
        {
            // Fixture setup
            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();

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

            Assert.NotEqual(exceptionToThrow, actualEx);
            // Teardown
        }
コード例 #20
0
        public void SpecimenRequestsAreEmptyAfterThrowing()
        {
            // Fixture setup
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();
            var tracer       = new DelegatingTracingBuilder(new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : new NoSpecimen()
            });
            var sut       = new TerminatingWithPathSpecimenBuilder(tracer);
            var container = new SpecimenContext(sut);

            // Exercise system and verify outcome
            Assert.Throws <ObjectCreationException>(() =>
                                                    sut.Create(firstRequest, container));

            Assert.Empty(sut.SpecimenRequests);
        }
コード例 #21
0
        public void SpecimenRequestsAreEmptyAfterThrowing()
        {
            // Arrange
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();
            var builder      = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : new NoSpecimen()
            };
            var sut = new TerminatingWithPathSpecimenBuilder(builder);
            // Cause sut to be executed recursively for multiple times.
            var container = new SpecimenContext(sut);

            // Act & assert
            Assert.ThrowsAny <ObjectCreationException>(() => sut.Create(firstRequest, container));

            Assert.Empty(sut.SpecimenRequests);
        }
コード例 #22
0
        public void ThrownExceptionIncludesInnerExceptionMessages()
        {
            // Arrange
            var innerInnerException = new InvalidOperationException("INNER_INNER_EXCEPTION");
            var innerException      = new InvalidOperationException("WRAPPED_INNER_EXCEPTION", innerInnerException);

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

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

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

            Assert.Contains("WRAPPED_INNER_EXCEPTION", actualEx.Message);
            Assert.Contains("INNER_INNER_EXCEPTION", actualEx.Message);
        }
    }
コード例 #23
0
        public void NestedExceptionIsWrappedForOneTimeOnly()
        {
            // Fixture setup
            var requests     = new[] { new object(), new object(), new object() };
            var requestQueue = new Queue <object>(requests);
            var firstRequest = requestQueue.Dequeue();

            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => requestQueue.Count > 0
                    ? c.Resolve(requestQueue.Dequeue())
                    : throw new InvalidOperationException()
            };

            var sut = new TerminatingWithPathSpecimenBuilder(builder);
            // Cause sut to be executed recursively for multiple times.
            var context = new SpecimenContext(sut);

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

            Assert.IsAssignableFrom <InvalidOperationException>(actualEx.InnerException);
            // Teardown
        }
コード例 #24
0
        public void CreateOnMultipleThreadsConcurrentlyWorks()
        {
            // Arrange
            var builder = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) => 99
            };
            var sut          = new TerminatingWithPathSpecimenBuilder(builder);
            var dummyContext = new DelegatingSpecimenContext
            {
                OnResolve = r => 99
            };

            // Act
            int[] specimens = Enumerable.Range(0, 1000)
                              .AsParallel()
                              .WithDegreeOfParallelism(8)
                              .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                              .Select(x => (int)sut.Create(typeof(int), dummyContext))
                              .ToArray();
            // Assert
            Assert.Equal(1000, specimens.Length);
            Assert.True(specimens.All(s => s == 99));
        }