Ejemplo n.º 1
0
        public void Instance_Returns_Same_Value_On_Multiple_Accesses()
        {
            // Arrange
            // Act
            using (var scope = new FactoryScoped <object>(
                       allocateFunction: () => { return(new object()); },
                       releaseAction: s => { }))
            {
                var firstAccess  = scope.Instance;
                var secondAccess = scope.Instance;

                // Assert
                secondAccess.Should().BeSameAs(firstAccess);
            }
        }
Ejemplo n.º 2
0
        public void AllocateFunction_Is_Not_Called_If_No_Access_To_Instance()
        {
            // Arrange
            var wasCalled = false;

            // Act
            using (var scope = new FactoryScoped <string>(
                       allocateFunction: () => { wasCalled = true; return("Hello World!"); },
                       releaseAction: s => { }))
            {
                wasCalled.Should().BeFalse();
            }

            // Assert
            wasCalled.Should().BeFalse();
        }
Ejemplo n.º 3
0
        public void ReleaseAction_Is_Called_On_Dispose()
        {
            // Arrange
            var wasCalled = false;

            // Act
            using (var scope = new FactoryScoped <string>(
                       allocateFunction: () => { return("Hello World!"); },
                       releaseAction: s => { wasCalled = true; }))
            {
                wasCalled.Should().BeFalse("releaseAction should not be called until Dispose() is called.");
                var thing = scope.Instance;
                scope.Dispose();

                // Assert
                wasCalled.Should().BeTrue();
            }
        }
Ejemplo n.º 4
0
        public void ReleaseAction_Is_Passed_The_Instance_Returned_From_FactoryFunction()
        {
            // Arrange
            var    theObjectToReturnFromFactory        = new object();
            object theObjectReturnedFromInstanceMember = null;
            object theObjectPassedToReleaseAction      = null;

            // Act
            using (var scope = new FactoryScoped <object>(
                       allocateFunction: () => { return(theObjectToReturnFromFactory); },
                       releaseAction: s => { theObjectPassedToReleaseAction = s; }))
            {
                theObjectReturnedFromInstanceMember = scope.Instance;
            }

            // Assert
            theObjectReturnedFromInstanceMember.Should().NotBeNull();
            theObjectReturnedFromInstanceMember.Should().BeSameAs(theObjectToReturnFromFactory);
            theObjectPassedToReleaseAction.Should().BeSameAs(theObjectReturnedFromInstanceMember);
        }