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 }
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 }
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); }
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); }
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); }
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)); }
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 }
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)); }
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 }
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); }
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 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); }
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 }
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 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 }
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"); }
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 }
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 }
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); }
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); }
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); } }
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 }
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)); }