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