public void BeginsLifetimeScope_WhenCurrentLifetimeScope_IsNull()
        {
            var query = new FakeQueryWithoutValidator();
            var decorated = new Mock<IHandleQuery<FakeQueryWithoutValidator, string>>(MockBehavior.Strict);
            decorated.Setup(x => x.Handle(query)).Returns("faked");
            var decorator = new QueryLifetimeScopeDecorator<FakeQueryWithoutValidator, string>(Container, () => decorated.Object);
            Container.GetCurrentLifetimeScope().ShouldEqual(null);

            var result = decorator.Handle(query);

            result.ShouldEqual("faked");
            decorated.Verify(x => x.Handle(query), Times.Once);
        }
        public void BeginsLifetimeScope_WhenCurrentLifetimeScope_IsNull()
        {
            var query     = new FakeQueryWithoutValidator();
            var decorated = new Mock <IHandleQuery <FakeQueryWithoutValidator, string> >(MockBehavior.Strict);

            decorated.Setup(x => x.Handle(query)).Returns("faked");

            var decorator = new QueryLifetimeScopeDecorator <FakeQueryWithoutValidator, string>(_fixture.Container, () => decorated.Object);

            Assert.Equal(null, _fixture.Container.GetCurrentLifetimeScope());

            var result = decorator.Handle(query);

            Assert.Equal("faked", result);
            decorated.Verify(x => x.Handle(query), Times.Once);
        }
        public void Handle_HasCurrentLifetimeScope_ShouldHandleQuery(
            [Frozen] Mock<IContainer> container,
            [Frozen] Mock<IQueryHandler<IQuery<object>, object>> queryHandler,
            IQuery<object> query,
            QueryLifetimeScopeDecorator<IQuery<object>, object> decorator)
        {
            // Act

            var result = decorator.Handle(query);

            // Assert

            result.Should().Not.Be(null);

            queryHandler.Verify(h => h.Handle(query), Times.Once);

            container.Verify(c => c.BeginLifetimeScope(), Times.Never);
        }
        public void UsesCurrentLifetimeScope_WhenCurrentLifetimeScope_IsNotNull()
        {
            var query = new FakeQueryWithoutValidator();
            var decorated = new Mock<IHandleQuery<FakeQueryWithoutValidator, string>>(MockBehavior.Strict);
            decorated.Setup(x => x.Handle(query)).Returns("faked");

            var decorator = new QueryLifetimeScopeDecorator<FakeQueryWithoutValidator, string>(_fixture.Container, () => decorated.Object);
            Assert.Equal(null, _fixture.Container.GetCurrentLifetimeScope());

            string result;
            using (_fixture.Container.BeginLifetimeScope())
            {
                result = decorator.Handle(query);
            }

            Assert.Equal("faked", result);

            decorated.Verify(x => x.Handle(query), Times.Once);
        }
        public void Handle_HasNoCurrentLifetimeScope_ShouldBeginNewLifetimeScopeBeforeHandleQuery(
            [Frozen] Mock<IContainer> container,
            [Frozen] Mock<IQueryHandler<IQuery<object>, object>> queryHandler,
            IQuery<object> query,
            QueryLifetimeScopeDecorator<IQuery<object>, object> decorator)
        {
            // Arrange

            var callOrder = 0;

            container.Setup(c => c.GetCurrentLifetimeScope()).Returns(null);
            container.Setup(c => c.BeginLifetimeScope()).Callback(() => callOrder++.Should().Be(0));
            queryHandler.Setup(c => c.Handle(query)).Callback(() => callOrder++.Should().Be(1));

            // Act

            decorator.Handle(query);

            // Assert

            container.Verify(c => c.BeginLifetimeScope(), Times.Once);

            queryHandler.Verify(h => h.Handle(query), Times.Once);
        }