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 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 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 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 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 SutIsISpecimenBuilderNode() { // Arrange // Act var sut = new TerminatingWithPathSpecimenBuilder(new DelegatingSpecimenBuilder()); // Assert Assert.IsAssignableFrom <ISpecimenBuilderNode>(sut); }
public void SpecimenRequestsWillInitiallyBeEmpty() { // Arrange var builder = new DelegatingSpecimenBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(builder); // Act & assert Assert.Empty(sut.SpecimenRequests); }
public void SutIsISpecimenBuilderNode() { // Fixture setup // Exercise system var sut = new TerminatingWithPathSpecimenBuilder(new DelegatingTracingBuilder()); // Verify outcome Assert.IsAssignableFrom <ISpecimenBuilderNode>(sut); // Teardown }
public void SpecimenRequestsWillInitiallyBeEmpty() { // Fixture setup var tracer = new DelegatingTracingBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(tracer); // Exercise system and verify outcome Assert.Empty(sut.SpecimenRequests); // Teardown }
public void SpecimenBuilderIsCorrect() { // Arrange var expected = new DelegatingSpecimenBuilder(); // Act var sut = new TerminatingWithPathSpecimenBuilder(expected); // Assert Assert.Same(expected, sut.Builder); }
public void TracerIsCorrect() { // Fixture setup var expected = new DelegatingTracingBuilder(); // Exercise system var sut = new TerminatingWithPathSpecimenBuilder(expected); // Verify outcome Assert.Same(expected, sut.Tracer); // Teardown }
public void SutYieldsCorrectSequence() { // Arrange var expected = new DelegatingSpecimenBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(expected); // Act // Assert Assert.Equal(expected, sut.Single()); Assert.Equal(expected, sut.Cast <object>().Single()); }
public void SutYieldsCorrectSequence() { // Fixture setup var expected = new DelegatingSpecimenBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(expected); // Exercise system // Verify outcome Assert.Equal(expected, sut.Single()); Assert.Equal(expected, sut.Cast <object>().Single()); // Teardown }
public void ComposeSingleItemReturnsCorrectResult() { // Arrange var builder = new DelegatingSpecimenBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(builder); // Act var expected = new DelegatingSpecimenBuilder(); var actual = sut.Compose(new[] { expected }); // Assert var tw = Assert.IsAssignableFrom <TerminatingWithPathSpecimenBuilder>(actual); Assert.Equal(expected, tw.Builder); }
public void SpecimenRequestsRaisedFromTracerAreRecordedCorrectly() { // Fixture setup var tracer = new DelegatingTracingBuilder(); var specimens = new[] { new object(), new object(), new object() }; var requestEvents = specimens.Select((o, i) => new RequestTraceEventArgs(o, i)).ToList(); var sut = new TerminatingWithPathSpecimenBuilder(tracer); // Exercise system requestEvents.ForEach(tracer.RaiseSpecimenRequested); // Verify outcome Assert.True(specimens.SequenceEqual(sut.SpecimenRequests)); // Teardown }
public void ComposeSingleItemReturnsCorrectResult() { // Fixture setup var tracer = new DelegatingTracingBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(tracer); // Exercise system var expected = new DelegatingSpecimenBuilder(); var actual = sut.Compose(new[] { expected }); // Verify outcome var tw = Assert.IsAssignableFrom <TerminatingWithPathSpecimenBuilder>(actual); Assert.Equal(expected, tw.Tracer.Builder); // 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 SpecimenRequestsAreEmptyWhenAllThatWereRequestedAreCreated() { // Fixture setup var tracer = new DelegatingTracingBuilder(); var specimens = new[] { new object(), new object(), new object() }; var requestEvents = specimens.Select( (o, i) => new RequestTraceEventArgs(o, i)).ToList(); var createdEvents = specimens.Reverse().Select( (o, i) => new SpecimenCreatedEventArgs(o, null, i)).ToList(); var sut = new TerminatingWithPathSpecimenBuilder(tracer); // Exercise system requestEvents.ForEach(tracer.RaiseSpecimenRequested); createdEvents.ForEach(tracer.RaiseSpecimenCreated); // Verify outcome Assert.Empty(sut.SpecimenRequests); // Teardown }
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 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 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 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 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 ComposeReturnsCorrectResult() { // Arrange var builder = new DelegatingSpecimenBuilder(); var sut = new TerminatingWithPathSpecimenBuilder(builder); // Act var expectedBuilders = new[] { new DelegatingSpecimenBuilder(), new DelegatingSpecimenBuilder(), new DelegatingSpecimenBuilder() }; var actual = sut.Compose(expectedBuilders); // Assert var tw = Assert.IsAssignableFrom <TerminatingWithPathSpecimenBuilder>(actual); var composite = Assert.IsAssignableFrom <CompositeSpecimenBuilder>(tw.Builder); Assert.True(expectedBuilders.SequenceEqual(composite)); }
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 }