コード例 #1
0
        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();
        }
コード例 #2
0
        public void Throws_exception_if_parameters_cannot_be_faked()
        {
            // When
            Action autoFakingObjectWithUnfakeableDependencies = () => An.AutoFaked <ObjectWithUnfakeableDependencies>();

            // Then
            autoFakingObjectWithUnfakeableDependencies.Should().Throw <AutoFakeCreationException>().Which.Message.Should().Contain("no constructor had parameters that were all fakeable");
        }
コード例 #3
0
        public void Throws_exception_if_type_has_no_public_constructor()
        {
            // When
            Action autoFakingObjectWithNoPublicConstructor = () => An.AutoFaked <ObjectWithNoPublicConstructor>();

            // Then
            autoFakingObjectWithNoPublicConstructor.Should().Throw <AutoFakeCreationException>().Which.Message.Should().Contain("no public constructor");
        }
コード例 #4
0
        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");
        }
コード例 #5
0
        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();
        }
コード例 #6
0
 public void Constructor_with_most_parameters_is_preferred()
 {
     An.AutoFaked <ObjectWithMultipleConstructors>().WasMadeFromConstructorWithMostParameters.Should().BeTrue();
 }
コード例 #7
0
 public void Auto_faked_dependencies_are_fakes()
 {
     An.AutoFaked <Foo>().Bar.Should().BeFake <IBar>();
 }
コード例 #8
0
 public void Can_auto_fake_object_with_fakeable_dependencies()
 {
     An.AutoFaked <ObjectWithFakeableDependencies>().Should().BeOfType <ObjectWithFakeableDependencies>();
 }
コード例 #9
0
        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>();
        }