public void Fakes_are_registered_weakly_and_so_NOT_eligible_for_garbage_collection() { // Given var foo = An.AutoFaked <Foo>(); var bar = TheFake <IBar> .UsedBy(foo); // WeakReference Class Definition (https://docs.microsoft.com/en-us/dotnet/api/system.weakreference?view=netframework-4.8): // References an object while still allowing that object to be reclaimed by garbage collection. // If an object is reclaimed for garbage collection, a new data object is regenerated; // ...otherwise, the object is available to access because of the weak reference var weakReferenceToFoo = new WeakReference(foo); var weakReferenceToBar = new WeakReference(bar); // Then weakReferenceToFoo.Target.Should().Be(foo); weakReferenceToBar.Target.Should().Be(bar); // When foo = null; bar = null; GC.Collect(); // Then weakReferenceToFoo.Target.Should().NotBe(foo); weakReferenceToBar.Target.Should().NotBe(bar); weakReferenceToFoo.Target.Should().NotBeNull(); weakReferenceToBar.Target.Should().NotBeNull(); }
public void Throws_exception_if_trying_to_retrieve_fakes_for_object_that_was_not_auto_faked() { // Given var foo = new Foo(A.Fake <IBar>()); // When Action retrivingFakeBar = () => TheFake <IBar> .UsedBy(foo); // Then retrivingFakeBar.Should().Throw <FakeRetrievalException>().Which.Message.Should().Contain("was not auto faked"); }
public void Exception_thrown_if_no_fake_is_registered_for_type() { // Given var foo = An.AutoFaked <Foo>(); // When Action retrievingBaz = () => TheFake <IBaz> .UsedBy(foo); // Then retrievingBaz.Should().Throw <FakeRetrievalException>().Which.Message.Should().Contain("did not use a fake of type"); }
public void Fakes_are_registered_weakly_and_so_eligible_for_garbage_collection() { // Given var foo = An.AutoFaked <Foo>(); var bar = TheFake <IBar> .UsedBy(foo); var weakReferenceToFoo = new WeakReference(foo); var weakReferenceToBar = new WeakReference(bar); // When // ReSharper disable RedundantAssignment foo = null; bar = null; // ReSharper restore RedundantAssignment GC.Collect(); // Then weakReferenceToFoo.Target.Should().BeNull(); weakReferenceToBar.Target.Should().BeNull(); }
public void Can_retrieve_faked_dependencies_on_auto_faked_object() { var foo = An.AutoFaked <Foo>(); TheFake <IBar> .UsedBy(foo).Should().Be(foo.Bar).And.BeFake <IBar>(); }