예제 #1
0
        public void DepthWillBeResetAfterDecoratedBuilderThrows()
        {
            // Arrange
            int createCallNumber = 0;
            int lastRequestDepth = 0;
            var firstRequest     = Guid.NewGuid();
            var secondRequest    = Guid.NewGuid();
            var dummyContainer   = new DelegatingSpecimenContext();
            var builder          = new DelegatingSpecimenBuilder
            {
                OnCreate = (r, c) =>
                {
                    createCallNumber++;
                    if (createCallNumber == 1)
                    {
                        throw new PrivateException();
                    }
                    return(c.Resolve(r));
                }
            };
            var sut = new DelegatingTracingBuilder(builder);

            sut.SpecimenRequested += (sender, e) => lastRequestDepth = e.Depth;

            // Act & assert
            Assert.Throws <PrivateException>(() => sut.Create(firstRequest, dummyContainer));
            Assert.Equal(1, lastRequestDepth);

            sut.Create(secondRequest, dummyContainer);
            Assert.Equal(1, lastRequestDepth);
        }
예제 #2
0
        public void CreateWillCorrectRaiseSpecimenCreatedInCompositeRequest()
        {
            // Fixture setup
            object request         = "The request";
            object subRequest      = "Some sub request";
            var    createdSpecimen = new object();

            var spy      = new List <SpecimenCreatedEventArgs>();
            var builder2 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == request?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? createdSpecimen : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenCreated += (sender, e) => spy.Add(e);

            var container = new SpecimenContext(sut);

            // Exercise system
            sut.Create(request, container);
            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(createdSpecimen, spy[0].Specimen);
            Assert.Equal(2, spy[0].Depth);
            Assert.Equal(createdSpecimen, spy[1].Specimen);
            Assert.Equal(1, spy[1].Depth);
            // Teardown
        }
예제 #3
0
        public void CreateWillCorrectlyRaiseSpecimenRequestedInCompositeRequest()
        {
            // Arrange
            object requestedObject = "The request";
            object subRequest      = "Some sub request";

            var spy      = new List <RequestTraceEventArgs>();
            var builder2 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == requestedObject?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? new object() : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenRequested += (sender, e) => spy.Add(e);

            var container = new SpecimenContext(sut);

            // Act
            sut.Create(requestedObject, container);
            // Assert
            Assert.Equal(2, spy.Count);
            Assert.Equal(subRequest, spy[1].Request);
            Assert.Equal(2, spy[1].Depth);
        }
예제 #4
0
        public void CreateWillTrackCompositeRequests()
        {
            // Fixture setup
            object requestedObject = "The request";
            object subRequest      = "Some sub request";
            var    spy             = new List <object>();
            var    builder2        = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == requestedObject?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? new object() : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenRequested += (sender, e) => spy.Add(e.Request);
            var container = new SpecimenContext(sut);

            // Exercise system
            sut.Create(requestedObject, container);

            // Verify outcome
            Assert.Equal(2, spy.Count);
            Assert.Equal(subRequest, spy[1]);

            // Teardown
        }
예제 #5
0
        public void CreateWillNotRaiseSpecimenCreatedForIgnoredType()
        {
            // Fixture setup
            var eventRaised = false;

            var request = Guid.NewGuid();
            var sut     = new DelegatingTracingBuilder();

            sut.SpecimenCreated += (sender, e) => eventRaised = true;

            var ignoredTypes = new List <Type> {
                typeof(Guid)
            };

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

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }
예제 #6
0
        public void IgnoredTypeWillNotTrackCreatedSpecimen()
        {
            // Arrange
            object tracked         = null;
            object requestedObject = Guid.NewGuid();

            var decoratedBuilder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r
            };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

            var ignoredTypes = new List <Type> {
                typeof(Guid)
            };

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType())
            };

            // Act
            var    dummyContainer = new DelegatingSpecimenContext();
            object res            = sut.Create(requestedObject, dummyContainer);

            // Assert
            Assert.Null(tracked);
        }
예제 #7
0
        public void CreateWillTrackCreatedSpecimensComposite()
        {
            // Arrange
            object requestedObject = "The request";
            object subRequest      = "Some sub request";
            object createdSpecimen = Guid.NewGuid();
            var    spy             = new List <object>();
            var    builder2        = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == requestedObject?c.Resolve(subRequest) : new NoSpecimen()
            };
            var builder3 = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r == subRequest ? createdSpecimen : new NoSpecimen()
            };
            var compBuilder = new CompositeSpecimenBuilder(builder2, builder3);

            var sut = new DelegatingTracingBuilder(compBuilder);

            sut.SpecimenCreated += (sender, e) => spy.Add(e.Specimen);
            var container = new SpecimenContext(sut);

            // Act
            sut.Create(requestedObject, container);

            // Assert
            Assert.Equal(2, spy.Count);
            Assert.Equal(createdSpecimen, spy[0]);
            Assert.Equal(createdSpecimen, spy[1]);
        }
예제 #8
0
        public void IgnoredTypeWillNotTrackRequest()
        {
            // Fixture setup
            object tracked         = null;
            object requestedObject = Guid.NewGuid();
            var    sut             = new DelegatingTracingBuilder();

            var ignoredTypes = new List <Type> {
                typeof(Guid)
            };

            sut.Filter = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => !ignoredTypes.Contains(r.GetType())
            };

            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(requestedObject, dummyContainer);

            // Verify outcome
            Assert.Null(tracked);

            // Teardown
        }
예제 #9
0
        public void CreateWillCorrectlyRaiseSpecimenRequested()
        {
            // Arrange
            var verified = false;
            var request  = new object();
            var sut      = new DelegatingTracingBuilder();

            sut.SpecimenRequested += (sender, e) => verified = e.Request == request && e.Depth == 1;
            // Act
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Assert
            Assert.True(verified, "Event raised");
        }
예제 #10
0
        public void CreateWillTrackCreatedSpecimen()
        {
            // Arrange
            object tracked         = null;
            object createdSpecimen = Guid.NewGuid();
            var    container       = new DelegatingSpecimenContext {
                OnResolve = r => createdSpecimen
            };
            var sut = new DelegatingTracingBuilder();

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

            // Assert
            Assert.Equal(res, tracked);
        }
예제 #11
0
        public void CreateWillPassThroughToDecoratedBuilder()
        {
            // Arrange
            object expectedSpecimen = Guid.NewGuid();
            var    decoratedBuilder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) => r
            };
            var sut = new DelegatingTracingBuilder(decoratedBuilder);

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

            // Assert
            Assert.Equal(expectedSpecimen, result);
        }
예제 #12
0
        public void CreateWillCorrectlyRaiseSpecimenRequested()
        {
            // Fixture setup
            var verified = false;
            var request  = new object();
            var sut      = new DelegatingTracingBuilder();

            sut.SpecimenRequested += (sender, e) => verified = e.Request == request && e.Depth == 1;
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.True(verified, "Event raised");
            // Teardown
        }
예제 #13
0
        public void CreateWillNotTrackFilteredRequest()
        {
            // Arrange
            object tracked         = null;
            object requestedObject = new object();
            var    sut             = new DelegatingTracingBuilder();

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

            sut.Create(requestedObject, dummyContainer);
            // Assert
            Assert.Null(tracked);
        }
예제 #14
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
        }
예제 #15
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
        }
예제 #16
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
        }
예제 #17
0
        public void CreateWillNotRaiseSpecimenCreatedForFilteredRequest()
        {
            // Arrange
            var eventRaised = false;

            var request = new object();
            var sut     = new DelegatingTracingBuilder();

            sut.SpecimenCreated += (sender, e) => eventRaised = true;
            sut.Filter           = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            // Act
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Assert
            Assert.False(eventRaised, "Event raised");
        }
예제 #18
0
        public void CreateWillNotRaiseSpecimenCreatedForFilteredRequest()
        {
            // Fixture setup
            var eventRaised = false;

            var request = new object();
            var sut     = new DelegatingTracingBuilder();

            sut.SpecimenCreated += (sender, e) => eventRaised = true;
            sut.Filter           = new DelegatingRequestSpecification {
                OnIsSatisfiedBy = r => false
            };
            // Exercise system
            var dummyContainer = new DelegatingSpecimenContext();

            sut.Create(request, dummyContainer);
            // Verify outcome
            Assert.False(eventRaised, "Event raised");
            // Teardown
        }