public void GetCurrentScope_CalledDuringVerificationOnADifferentThread_ReturnsARealScope() { // Arrange Scope backgroundRequestedScope = null; Task task = null; var container = new Container(); var scope = new Scope(); var lifestyle = new FakeScopedLifestyle(scope); container.Register <IPlugin, DisposablePlugin>(lifestyle); container.RegisterInitializer <DisposablePlugin>(p => { // Resolve on a different thread (so not during verification) task = Task.Run(() => { backgroundRequestedScope = lifestyle.GetCurrentScope(container); }); Thread.Sleep(150); }); // Act container.Verify(); task.Wait(); // Assert Assert.AreSame(scope, backgroundRequestedScope, "Since the background thread does not run verify, the returned scope should not be the " + "verification scope."); }
public void GetCurrentScope_CalledOutsideOfVerification_ReturnsNull() { // Arrange var scopedLifestyle = new FakeScopedLifestyle(scope: null); // Act var actualScope = scopedLifestyle.GetCurrentScope(new Container()); // Assert Assert.IsNull(actualScope); }
public void GetCurrentScope_CalledDuringVerification_ReturnsAScope() { // Arrange Scope scopeDuringVerification = null; var scopedLifestyle = new FakeScopedLifestyle(scope: null); var container = new Container(); container.Register <IDisposable>(() => new DisposableObject(), scopedLifestyle); container.RegisterInitializer <IDisposable>(_ => { scopeDuringVerification = scopedLifestyle.GetCurrentScope(container); }); // Act container.Verify(); // Assert Assert.IsNotNull(scopeDuringVerification); }
public void GetCurrentScope_CalledOnSameThreadDuringVerification_ReturnsTheVerificationScope() { // Arrange Scope verificationScope = null; var container = new Container(); var scope = new Scope(); var scopedLifestyle = new FakeScopedLifestyle(scope); container.Register <IPlugin, DisposablePlugin>(scopedLifestyle); container.RegisterInitializer <DisposablePlugin>(p => { verificationScope = scopedLifestyle.GetCurrentScope(container); }); // Act // When calling verify, we expect DisposablePlugin to be created again. container.Verify(); // Assert Assert.AreNotSame(verificationScope, scope); Assert.IsTrue(verificationScope.ToString().Contains("VerificationScope")); }
public void GetCurrentScope_CalledDuringVerificationOnADifferentThread_ReturnsARealScope() { // Arrange Scope backgroundRequestedScope = null; Task task = null; var container = new Container(); var scope = new Scope(); var lifestyle = new FakeScopedLifestyle(scope); container.Register<IPlugin, DisposablePlugin>(lifestyle); container.RegisterInitializer<DisposablePlugin>(p => { // Resolve on a different thread (so not during verification) task = Task.Run(() => { backgroundRequestedScope = lifestyle.GetCurrentScope(container); }); Thread.Sleep(150); }); // Act container.Verify(); task.Wait(); // Assert Assert.AreSame(scope, backgroundRequestedScope, "Since the background thread does not run verify, the returned scope should not be the " + "verification scope."); }
public void GetCurrentScope_CalledOnSameThreadDuringVerification_ReturnsTheVerificationScope() { // Arrange Scope verificationScope = null; var container = new Container(); var scope = new Scope(); var scopedLifestyle = new FakeScopedLifestyle(scope); container.Register<IPlugin, DisposablePlugin>(scopedLifestyle); container.RegisterInitializer<DisposablePlugin>(p => { verificationScope = scopedLifestyle.GetCurrentScope(container); }); // Act // When calling verify, we expect DisposablePlugin to be created again. container.Verify(); // Assert Assert.AreNotSame(verificationScope, scope); Assert.IsTrue(verificationScope.ToString().Contains("VerificationScope")); }
public void GetCurrentScope_CalledDuringVerification_ReturnsAScope() { // Arrange Scope scopeDuringVerification = null; var scopedLifestyle = new FakeScopedLifestyle(scope: null); var container = new Container(); container.Register<IDisposable>(() => new DisposableObject(), scopedLifestyle); container.RegisterInitializer<IDisposable>(_ => { scopeDuringVerification = scopedLifestyle.GetCurrentScope(container); }); // Act container.Verify(); // Assert Assert.IsNotNull(scopeDuringVerification); }