Exemplo n.º 1
0
        public void UseSuccessfulConstructor(
            FakedClass fake)
        {
            "Given a class with multiple constructors"
            .See <FakedClass>();

            "And the parameterless constructor throws"
            .See(() => new FakedClass());

            "And the class has a one-parameter constructor"
            .See(() => new FakedClass(A.Dummy <IDisposable>()));

            "And the class has a two-parameter constructor"
            .See(() => new FakedClass(A.Dummy <IDisposable>(), A.Dummy <string>()));

            "When I create a fake of the class"
            .x(() => fake = this.CreateFake <FakedClass>());

            "Then the fake is instantiated using the two-parameter constructor"
            .x(() => fake.WasTwoParameterConstructorCalled.Should().BeTrue());

            "And the fake doesn't remember the failing constructor call"
            .x(() => fake.WasParameterlessConstructorCalled
               .Should().BeFalse("because the parameterless constructor was called for a different fake object"));

            "And the one-parameter constructor was not tried"
            .x(() => FakedClass.ParameterListLengthsForAttemptedConstructors.Should().BeEquivalentTo(0, 2));
        }
Exemplo n.º 2
0
        public static void EqualsSameFake(FakedClass fake, bool equalsResult)
        {
            "Given a faked class instance"
            .x(() => fake = A.Fake <FakedClass>());

            "When I compare it to itself using Equals"
            .x(() => equalsResult = fake.Equals(fake));

            "Then it compares as equal"
            .x(() => equalsResult.Should().BeTrue());
        }
Exemplo n.º 3
0
        public static void ToStringDescribesFake(FakedClass fake, string toStringValue)
        {
            "Given a faked class instance"
            .x(() => fake = A.Fake <FakedClass>());

            "When I call ToString on it"
            .x(() => toStringValue = fake.ToString());

            "Then it indicates that it's a fake"
            .x(() => toStringValue.Should().Be("Faked FakeItEasy.Specs.UnconfiguredFakeSpecs+FakedClass"));
        }
Exemplo n.º 4
0
        public void UseSuccessfulConstructor(
            FakedClass fake,
            IEnumerable <int> parameterListLengthsForAttemptedConstructors)
        {
            "Given a class with multiple constructors"
            .See <FakedClass>();

            "And the parameterless constructor throws"
            .See(() => new FakedClass());

            "And the class has a one-parameter constructor"
            .See(() => new FakedClass(default));
Exemplo n.º 5
0
        public static void EqualsDifferentInstanceOfSameFakedType(FakedClass fake1, FakedClass fake2, bool equalsResult)
        {
            "Given a faked class instance"
            .x(() => fake1 = A.Fake <FakedClass>());

            "And a second faked class instance of the same type"
            .x(() => fake2 = A.Fake <FakedClass>());

            "When I compare the instances using Equals"
            .x(() => equalsResult = fake1.Equals(fake2));

            "Then they compare as unequal"
            .x(() => equalsResult.Should().BeFalse());
        }
Exemplo n.º 6
0
        public static void UseSuccessfulConstructor(
            FakedClass fake)
        {
            "When faking a class whose first constructor fails"
            .x(() => fake = A.Fake <FakedClass>());

            "It should instantiate the fake using the successful constructor with the longest parameter list"
            .x(() => fake.WasTwoParameterConstructorCalled.Should().BeTrue());

            "And the fake should not remember the failing constructor call"
            .x(() => fake.WasParameterlessConstructorCalled
               .Should().BeFalse("because the parameterless constructor was called for a different fake object"));

            "And it should only have tried the parameterless constructor and one with the longest parameter list"
            .x(() => FakedClass.ParameterListLengthsForAttemptedConstructors.Should().BeEquivalentTo(0, 2));
        }
        public static void FakeableProperty(
            FakedClass fake,
            IFoo result)
        {
            "Given a type with a virtual fakeable-type property"
            .x(() => { });     // see FakedClasss

            "And a fake of that type"
            .x(() => fake = A.Fake <FakedClass>());

            "When I get the property value"
            .x(() => result = fake.FakeableProperty);

            "Then the value will be a Dummy"
            .x(() => result.IsADummy.Should().BeTrue("because the property value should be a Dummy"));
        }
Exemplo n.º 8
0
        public void UseSuccessfulConstructor(
            FakedClass fake)
        {
            "when faking a class whose first constructor fails"
                .x(() => fake = A.Fake<FakedClass>());

            "it should instantiate the fake using the successful constructor with the longest parameter list"
                .x(() => fake.WasTwoParameterConstructorCalled.Should().BeTrue());

            "it should instantiate a fake that does not remember the failing constructor call"
                .x(() => fake.WasParameterlessConstructorCalled
                             .Should().BeFalse("because the parameterless constructor was called for a different fake object"));

            "it should only have tried the parameterless constructor and one with the longest parameter list"
                .x(() => FakedClass.ParameterListLengthsForAttemptedConstructors.Should().BeEquivalentTo(0, 2));
        }
Exemplo n.º 9
0
        public void VirtualProperties(
            FakedClass fake)
        {
            "when faking a class with virtual properties"
                .x(() => fake = A.Fake<FakedClass>());

            "it should return a default value when a reference type property is called during the constructor"
                .x(() => fake.StringPropertyValueDuringConstructorCall.Should().Be(string.Empty));

            // The test for the default value of a value type is a regression test for https://github.com/FakeItEasy/FakeItEasy/issues/368:
            "it should return a default value when a value type property is called during the constructor"
                .x(() => fake.ValueTypePropertyValueDuringConstructorCall.Should().Be(0));

            "it should return the value assigned during constructor when a reference type property is gotten after the constructor"
                .x(() => fake.StringProperty.Should().Be("value set in constructor"));

            "it should return the value assigned during constructor when a value type property is gotten after the constructor"
                .x(() => fake.ValueTypeProperty.Should().Be(123456));
        }
        public static void VirtualReferenceTypeProperty(
            FakedClass fake,
            string result)
        {
            "Given a type with a default constructor"
            .x(() => { });     // see FakedClass

            "And the constructor assigns a value to a virtual reference-type property"
            .x(() => { });     // see FakedClass.ctor()

            "And a fake of that type"
            .x(() => fake = A.Fake <FakedClass>());

            "When I fetch the property value"
            .x(() => result = fake.StringProperty);

            "Then it will be the value assigned during construction"
            .x(() => result.Should().Be("value set in constructor"));
        }
Exemplo n.º 11
0
        public static void VirtualProperties(
            FakedClass fake)
        {
            "when faking a class with virtual properties"
            .x(() => fake = A.Fake <FakedClass>());

            "it should return a default value when a reference type property is called during the constructor"
            .x(() => fake.StringPropertyValueDuringConstructorCall.Should().Be(string.Empty));

            // The test for the default value of a value type is a regression test for https://github.com/FakeItEasy/FakeItEasy/issues/368:
            "it should return a default value when a value type property is called during the constructor"
            .x(() => fake.ValueTypePropertyValueDuringConstructorCall.Should().Be(0));

            "it should return the value assigned during constructor when a reference type property is gotten after the constructor"
            .x(() => fake.StringProperty.Should().Be("value set in constructor"));

            "it should return the value assigned during constructor when a value type property is gotten after the constructor"
            .x(() => fake.ValueTypeProperty.Should().Be(123456));
        }
        public static void VirtualPropertiesCalledDuringConstruction(
            FakedClass fake)
        {
            "Given a type with a default constructor"
            .x(() => { });     // see FakedClass

            "And the constructor calls some virtual properties"
            .x(() => { });     // see FakedClass.ctor()

            "And the properties return non-default values"
            .x(() => { });    // see FakedClass.StringProperty, FakedClass.ValueTypeProperty

            "When I create a fake of the type"
            .x(() => fake = A.Fake <FakedClass>());

            "Then the reference-type property will return a default value"
            .x(() => fake.StringPropertyValueDuringConstructorCall.Should().Be(string.Empty));

            "And the value-type property will return a default value"
            .x(() => fake.ValueTypePropertyValueDuringConstructorCall.Should().Be(0));
        }
Exemplo n.º 13
0
        public void UseSuccessfulConstructor(
            FakedClass fake,
            IEnumerable <int> parameterListLengthsForAttemptedConstructors)
        {
            "Given a class with multiple constructors"
            .See <FakedClass>();

            "And the parameterless constructor throws"
            .See(() => new FakedClass());

            "And the class has a one-parameter constructor"
            .See(() => new FakedClass(new ArgumentThatShouldNeverBeResolved()));

            "And the class has a two-parameter constructor"
            .See(() => new FakedClass(A.Dummy <IDisposable>(), string.Empty));

            "When I create a fake of the class"
            .x(() =>
            {
                lock (FakedClass.ParameterListLengthsForAttemptedConstructors)
                {
                    FakedClass.ParameterListLengthsForAttemptedConstructors.Clear();
                    fake = this.CreateFake <FakedClass>();
                    parameterListLengthsForAttemptedConstructors = new List <int>(FakedClass.ParameterListLengthsForAttemptedConstructors);
                }
            });

            "Then the fake is instantiated using the two-parameter constructor"
            .x(() => fake.WasTwoParameterConstructorCalled.Should().BeTrue());

            "And the fake doesn't remember the failing constructor call"
            .x(() => fake.WasParameterlessConstructorCalled
               .Should().BeFalse("because the parameterless constructor was called for a different fake object"));

            "And the one-parameter constructor was not tried"
            .x(() => parameterListLengthsForAttemptedConstructors.Should().NotContain(1));

            "And the argument for the unused constructor was never resolved"
            .x(() => ArgumentThatShouldNeverBeResolved.WasResolved.Should().BeFalse());
        }