public static void ConfiguringSetterViaMethod(
            IHaveInterestingProperties subject,
            Exception exception)
        {
            "Given a Fake with a property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When assignment of the property is configured using a method call expression"
                .x(() => exception = Record.Exception(() =>
                    A.CallToSet(() => subject.MethodThatLooksLikeAPropertyGetter()).DoesNothing()));

            "Then an argument exception is thrown"
                .x(() => exception.Should().BeAnExceptionOfType<ArgumentException>());

            "And the exception message indicates that the expression refers to an incorrect member type"
                .x(() => exception.Message.Should().EndWith("' must refer to a property or indexer getter, but doesn't."));
        }
        public static void ConfiguringSetterForReadOnlyProperty(
            IHaveInterestingProperties subject,
            Exception exception)
        {
            "Given a Fake with a read-only property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When assignment of the property is configured"
                .x(() => exception = Record.Exception(() => A.CallToSet(() => subject.ReadOnlyProperty).DoesNothing()));

            "Then an argument exception is thrown"
                .x(() => exception.Should().BeAnExceptionOfType<ArgumentException>());

            "And the exception message indicates that the property is read-only"
                .x(() => exception.Message.Should().Be(
                    $"The property '{nameof(IHaveInterestingProperties.ReadOnlyProperty)}' does not have a setter."));
        }
        public static void OverridePropertyValueConfigurationWithWhenArgumentsMatchAndCallWithBadValue(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "And assignment of the property is configured for value 3 using WhenArgumentsMatch"
            .x(() => A.CallToSet(() => subject.ReadWriteProperty)
               .WhenArgumentsMatch(arguments => arguments.Get <int>(0) == 3)
               .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property value to 4"
            .x(() => subject.ReadWriteProperty = 4);

            "Then the configured behavior is not used"
            .x(() => wasConfiguredBehaviorUsed.Should().BeFalse());
        }
        public static void OverrideIndexerConfigurationWithWhenArgumentsMatch(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with an indexer"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "And assignment of the indexer is configured using WhenArgumentsMatch"
            .x(() => A.CallToSet(() => subject["Choeropsis", "liberiensis"])
               .WhenArgumentsMatch(arguments => arguments.Get <string>("genus") == "Canis")
               .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property using indexes that satisfy the WhenArgumentsMatch"
            .x(() => subject["Canis", "lupus"] = false);

            "Then the configured behavior is used"
            .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
Example #5
0
        public static void SettingIndexedPropertyForDifferentIndexes(
            IHaveInterestingProperties subject)
        {
            "establish"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "when setting the value of an indexed property for different indexes"
                .x(() =>
                    {
                        subject[17, true] = new List<string> { "hippo", "lemur" };
                        subject[17, false] = new List<string> { "corgi", "chicken" };
                    });

            "it should return the correct value for the first indexes"
                .x(() => subject[17, true].Should().BeEquivalentTo("hippo", "lemur"));

            "it should return the correct value for the second indexes"
                .x(() => subject[17, false].Should().BeEquivalentTo("corgi", "chicken"));
        }
Example #6
0
        public static void ConfiguringSetterWithValueSpecificationAndAssigningMatching(
            int actualValue,
            string fateOfConfiguredBehavior,
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "And assignment of the property is configured for values greater than 4"
            .x(() => A.CallToSet(() => subject.ReadWriteProperty).To(() => A <int> .That.IsGreaterThan(4))
               .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property to the value {0}"
            .x(() => subject.ReadWriteProperty = actualValue);

            "Then the configured behavior is {1}"
            .x(() => wasConfiguredBehaviorUsed.Should().Be(fateOfConfiguredBehavior == "used"));
        }
        public static void GettingUnconfiguredFakeableProperty(
            IHaveInterestingProperties subject,
            IHaveInterestingProperties firstGetResult,
            IHaveInterestingProperties secondGetResult)
        {
            "Given a Fake with a property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When I get the value of the property"
                .x(() => firstGetResult = subject.FakeableProperty);

            "And I get the value of the property again"
                .x(() => secondGetResult = subject.FakeableProperty);

            "Then the property does not return null the first time"
                .x(() => firstGetResult.Should().NotBeNull());

            "And the property returns the same instance when called again"
                .x(() => secondGetResult.Should().BeSameAs(firstGetResult));
        }
Example #8
0
        public static void GettingUnconfiguredFakeableProperty(
            IHaveInterestingProperties subject,
            IHaveInterestingProperties firstGetResult,
            IHaveInterestingProperties secondGetResult)
        {
            "Given a Fake with a property"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "When I get the value of the property"
            .x(() => firstGetResult = subject.FakeableProperty);

            "And I get the value of the property again"
            .x(() => secondGetResult = subject.FakeableProperty);

            "Then the property does not return null the first time"
            .x(() => firstGetResult.Should().NotBeNull());

            "And the property returns the same instance when called again"
            .x(() => secondGetResult.Should().BeSameAs(firstGetResult));
        }
Example #9
0
        public static void GettingUnconfiguredFakeableProperty(
            IHaveInterestingProperties subject,
            IHaveInterestingProperties firstValue,
            IHaveInterestingProperties secondValue)
        {
            "establish"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "when getting the value of an unconfigured fakeable property"
            .x(() =>
            {
                firstValue  = subject.FakeableProperty;
                secondValue = subject.FakeableProperty;
            });

            "it should not return null"
            .x(() => firstValue.Should().NotBeNull());

            "it should return the same instance on a subsequent get"
            .x(() => secondValue.Should().BeSameAs(firstValue));
        }
Example #10
0
        public void GettingUnconfiguredUnfakeableProperty(
            IHaveInterestingProperties subject,
            UnfakeableClass firstValue,
            UnfakeableClass secondValue)
        {
            "establish"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "when getting the value of an unconfigured unfakeable property"
                .x(() =>
                    {
                        firstValue = subject.UnfakeableProperty;
                        secondValue = subject.UnfakeableProperty;
                    });

            "it should not return null if dummy can be made"
                .x(() => firstValue.Should().NotBeNull());

            "it should return the same instance on a subsequent get"
                .x(() => secondValue.Should().BeSameAs(firstValue));
        }
Example #11
0
        public static void SettingIndexedProperty(
            IHaveInterestingProperties subject)
        {
            "establish"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "when setting the value of an indexed property"
            .x(() => subject[17, true] = new List <string> {
                "hippo", "lemur"
            });

            "it should return the value to the getter with same indexes"
            .x(() => subject[17, true].Should().BeEquivalentTo("hippo", "lemur"));

            "it should return the same instance each time the getter is called with those indexes"
            .x(() =>
            {
                var firstResult  = subject[17, true];
                var secondResult = subject[17, true];
                ReferenceEquals(firstResult, secondResult)
                .Should().BeTrue("property getters should return the same object every time");
            });

            "it should return the default value to getters with different indexes"
            .x(() =>
            {
                var result = subject[-183, true];
                result.Should().BeEmpty();
            });

            "it should return the same instance each time the getter is called with other indexes"
            .x(() =>
            {
                var firstResult  = subject[18, false];
                var secondResult = subject[18, false];
                ReferenceEquals(firstResult, secondResult)
                .Should().BeTrue("property getters should return the same object every time");
            });
        }
Example #12
0
        public static void ConfiguringSetterFromBaseInterfaceOnFakedClassFromInterfaceTypedVar(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given an interface with a read/write property"
            .See <IHaveInterestingProperties>();

            "And a class that implements it"
            .See <ClassImplementingInterfaceWithInterestingProperties>();

            "And a Fake created from the class but assigned to a variable of the interface's type"
            .x(() => subject = A.Fake <ClassImplementingInterfaceWithInterestingProperties>());

            "And assignment of the property is configured for any value"
            .x(() => A.CallToSet(() => subject.ReadWriteProperty).Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign a value to the property"
            .x(() => subject.ReadWriteProperty = 0);

            "Then the configured behavior is used"
            .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void GettingUnconfiguredPropertyOfUnfakeableType(
            IHaveInterestingProperties subject,
            UnfakeableClass firstGetResult,
            UnfakeableClass secondGetResult)
        {
            "Given a Fake with a property whose type is unfakeable"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And the property type can be made into a Dummy"
                .x(() => { }); // see UnfakeableClass

            "When I get the value of the property"
                .x(() => firstGetResult = subject.UnfakeableProperty);

            "And I get the value of the property again"
                .x(() => secondGetResult = subject.UnfakeableProperty);

            "Then the property does not return null if a Dummy can be made"
                .x(() => firstGetResult.Should().NotBeNull());

            "And the property returns the same instance when called again"
                .x(() => secondGetResult.Should().BeSameAs(firstGetResult));
        }
Example #14
0
        public static void SettingIndexedPropertyForDifferentIndexes(
            IHaveInterestingProperties subject)
        {
            "establish"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "when setting the value of an indexed property for different indexes"
            .x(() =>
            {
                subject[17, true] = new List <string> {
                    "hippo", "lemur"
                };
                subject[17, false] = new List <string> {
                    "corgi", "chicken"
                };
            });

            "it should return the correct value for the first indexes"
            .x(() => subject[17, true].Should().BeEquivalentTo("hippo", "lemur"));

            "it should return the correct value for the second indexes"
            .x(() => subject[17, false].Should().BeEquivalentTo("corgi", "chicken"));
        }
Example #15
0
        public static void GettingUnconfiguredPropertyOfUnfakeableType(
            IHaveInterestingProperties subject,
            UnfakeableClass firstGetResult,
            UnfakeableClass secondGetResult)
        {
            "Given a Fake with a property whose type is unfakeable"
            .x(() => subject = A.Fake <IHaveInterestingProperties>());

            "And the property type can be made into a Dummy"
            .x(() => { });     // see UnfakeableClass

            "When I get the value of the property"
            .x(() => firstGetResult = subject.UnfakeableProperty);

            "And I get the value of the property again"
            .x(() => secondGetResult = subject.UnfakeableProperty);

            "Then the property does not return null if a Dummy can be made"
            .x(() => firstGetResult.Should().NotBeNull());

            "And the property returns the same instance when called again"
            .x(() => secondGetResult.Should().BeSameAs(firstGetResult));
        }
Example #16
0
        public static void SettingIndexedProperty(
            IHaveInterestingProperties subject)
        {
            "establish"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "when setting the value of an indexed property"
                .x(() => subject[17, true] = new List<string> { "hippo", "lemur" });

            "it should return the value to the getter with same indexes"
                .x(() => subject[17, true].Should().BeEquivalentTo("hippo", "lemur"));

            "it should return the same instance each time the getter is called with those indexes"
                .x(() =>
                    {
                        var firstResult = subject[17, true];
                        var secondResult = subject[17, true];
                        ReferenceEquals(firstResult, secondResult)
                            .Should().BeTrue("property getters should return the same object every time");
                    });

            "it should return the default value to getters with different indexes"
                .x(() =>
                    {
                        var result = subject[-183, true];
                        result.Should().BeEmpty();
                    });

            "it should return the same instance each time the getter is called with other indexes"
                .x(() =>
                    {
                        var firstResult = subject[18, false];
                        var secondResult = subject[18, false];
                        ReferenceEquals(firstResult, secondResult)
                            .Should().BeTrue("property getters should return the same object every time");
                    });
        }
        public static void SettingIndexedPropertyForDifferentIndexes(
            IHaveInterestingProperties subject,
            List<string> getResultForFirstIndexes,
            List<string> getResultForSecondIndexes)
        {
            "Given a Fake with an indexed property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When I set the value of the property"
                .x(() => subject[17, true] = new List<string> { "hippo", "lemur" });

            "And I set the value of the property using different indexes"
                .x(() => subject[17, false] = new List<string> { "corgi", "chicken" });

            "And I call the getter with the original indexes"
                .x(() => getResultForFirstIndexes = subject[17, true]);

            "And I call the getter with the second indexes"
                .x(() => getResultForSecondIndexes = subject[17, false]);

            "Then the property returns the correct value when called with the original indexes"
                .x(() => getResultForFirstIndexes.Should().BeEquivalentTo("hippo", "lemur"));

            "And the property returns the correct value when called with the second indexes"
                .x(() => getResultForSecondIndexes.Should().BeEquivalentTo("corgi", "chicken"));
        }
        public static void ConfiguringSetterWithValueSpecificationAndAssigningMatching(
            int actualValue,
            string fateOfConfiguredBehavior,
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
               .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the property is configured for values greater than 4"
                .x(() => A.CallToSet(() => subject.ReadWriteProperty).To(() => A<int>.That.IsGreaterThan(4))
                    .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property to the value {0}"
                .x(() => subject.ReadWriteProperty = actualValue);

            "Then the configured behavior is {1}"
                .x(() => wasConfiguredBehaviorUsed.Should().Be(fateOfConfiguredBehavior == "used"));
        }
        public static void OverrideIndexerConfigurationWithWithAnyArguments(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with an indexer"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the indexer is configured for specific arguments and overridden with WithAnyArguments"
                .x(() => A.CallToSet(() => subject["Choeropsis", "liberiensis"])
                    .WithAnyArguments()
                    .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property using the wrong indexes"
                .x(() => subject["Eleoniscus", "helenae"] = false);

            "Then the configured behavior is used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void OverridePropertyValueConfigurationWithWhenArgumentsMatchAndCallWithBadValue(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the property is configured for value 3 using WhenArgumentsMatch"
                .x(() => A.CallToSet(() => subject.ReadWriteProperty)
                    .WhenArgumentsMatch(arguments => arguments.Get<int>(0) == 3)
                    .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property value to 4"
                .x(() => subject.ReadWriteProperty = 4);

            "Then the configured behavior is not used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeFalse());
        }
        public static void OverrideIndexerConfigurationWithWhenArgumentsMatch(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with an indexer"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the indexer is configured using WhenArgumentsMatch"
                .x(() => A.CallToSet(() => subject["Choeropsis", "liberiensis"])
                    .WhenArgumentsMatch(arguments => arguments.Get<string>("genus") == "Canis")
                    .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property using indexes that satisfy the WhenArgumentsMatch"
                .x(() => subject["Canis", "lupus"] = false);

            "Then the configured behavior is used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void ConfigureReadOnlyIndexer(
            IHaveInterestingProperties subject,
            Exception exception)
        {
            "Given a Fake with a read-only indexer"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When assignment of the indexer is configured"
                .x(() => exception = Record.Exception(() => A.CallToSet(() => subject[7]).DoesNothing()));

            "Then an argument exception is thrown"
               .x(() => exception.Should().BeAnExceptionOfType<ArgumentException>());

            "And the exception message indicates that the property is read only"
                .x(() => exception.Message.Should().EndWith("refers to an indexed property that does not have a setter."));
        }
        public static void SettingIndexedProperty(
            IHaveInterestingProperties subject,
            List<string> firstGetResultForOriginalIndexes,
            List<string> firstGetResultForDifferentIndexes)
        {
            "Given a Fake with an indexed property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "When I set the value of the property"
                .x(() => subject[17, true] = new List<string> { "hippo", "lemur" });

            "And I call the getter with the original indexes"
                .x(() => firstGetResultForOriginalIndexes = subject[17, true]);

            "And I call the getter with different indexes"
                .x(() => firstGetResultForDifferentIndexes = subject[-183, true]);

            "Then the property returns the supplied value when called using the original indexes"
                .x(() => firstGetResultForOriginalIndexes.Should().BeEquivalentTo("hippo", "lemur"));

            "And the property returns the same instance when called again with those indexes"
                .x(() => subject[17, true].Should()
                    .BeSameAs(firstGetResultForOriginalIndexes, "property getters should return the same object every time"));

            "And the property returns the default value when called using the different indexes"
                .x(() => firstGetResultForDifferentIndexes.Should().BeEmpty());

            "And the property returns the same instance when called again with those indexes"
                .x(() => subject[-183, true].Should().BeSameAs(firstGetResultForDifferentIndexes));
        }
        public static void ConfigureIndexerWithMatchingIndexes(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with an indexer"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the indexer is configured"
                .x(() => A.CallToSet(() => subject["Choeropsis", "liberiensis"]).Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property using matching indexes"
                .x(() => subject["Choeropsis", "liberiensis"] = false);

            "Then the configured behavior is used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void ConfiguringSetterForAnyValue(
            int value,
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the property is configured for any value"
                .x(() => A.CallToSet(() => subject.ReadWriteProperty).Invokes(call => wasConfiguredBehaviorUsed = true));

            $"When I assign the property to {value}"
                .x(() => subject.ReadWriteProperty = value);

            "Then the configured behavior is used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void ConfigureOverloadedIndexer(
            IHaveInterestingProperties subject,
            bool wasFirstConfiguredBehaviorUsed,
            bool wasSecondConfiguredBehaviorUsed)
        {
            "Given a Fake with an overloaded indexer"
                .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the indexer is configured for one signature"
                .x(() => A.CallToSet(() => subject["Choeropsis", "liberiensis"]).Invokes(call => wasFirstConfiguredBehaviorUsed = true));

            "And assignment of the indexer is configured for the other signature"
                .x(() => A.CallToSet(() => subject["Pygmy hippopotamus"]).Invokes(call => wasSecondConfiguredBehaviorUsed = true));

            "When I assign the property using one signature"
                .x(() => subject["Choeropsis", "liberiensis"] = true);

            "And I assign the property using the other signature"
                .x(() => subject["Pygmy hippopotamus"] = 4);

            "Then the configured behavior is used for the first signature"
                .x(() => wasFirstConfiguredBehaviorUsed.Should().BeTrue());

            "And the configured behavior is used for the second signature"
                .x(() => wasSecondConfiguredBehaviorUsed.Should().BeTrue());
        }
        public static void ConfiguringSetterWithExactValueAndAssigningDifferentValue(
            IHaveInterestingProperties subject,
            bool wasConfiguredBehaviorUsed)
        {
            "Given a Fake with a property"
               .x(() => subject = A.Fake<IHaveInterestingProperties>());

            "And assignment of the property is configured for a specific value"
                .x(() => A.CallToSet(() => subject.ReadWriteProperty).To(5)
                .Invokes(call => wasConfiguredBehaviorUsed = true));

            "When I assign the property to a different value"
                .x(() => subject.ReadWriteProperty = -13);

            "Then the configured behavior is not used"
                .x(() => wasConfiguredBehaviorUsed.Should().BeFalse());
        }