Ejemplo n.º 1
0
        public static void ConstraintFactoryThatThrows(
            IHaveAnObjectParameter fake,
            Func <object> constraintFactory,
            Exception exception1,
            Exception exception2)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IHaveAnObjectParameter>());

            "And a delegate throws while producing an argument constraint"
            .x(() => constraintFactory = () => throw new InvalidOperationException("I don't want to make a constraint"));

            "When I try to configure a method of the fake with this delegate"
            .x(() => exception1 = Record.Exception(() => A.CallTo(() => fake.Bar(constraintFactory())).Returns(1)));

            "And I try to create an argument constraint outside a call configuration"
            .x(() => exception2 = Record.Exception(() => A <int> .Ignored));

            "Then the first call configuration throws a UserCallbackException"
            .x(() => exception1.Should().BeAnExceptionOfType <UserCallbackException>()
               .WithInnerException <InvalidOperationException>());

            "And creation of the argument constraint throws an InvalidOperationException"
            .x(() => exception2.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
Ejemplo n.º 2
0
        public static void ConstraintFactoryThatMakesTwoConstraints(
            IHaveAnObjectParameter fake,
            Func <object> constraintFactory,
            Exception exception)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IHaveAnObjectParameter>());

            "And a delegate that produces two constraints"
            .x(() => constraintFactory = () =>
            {
                A <object> .That.Matches(i => i is object);
                return(A <object> .That.Matches(i => i is object));
            });

            "When I try to configure a method of the fake with this delegate"
            .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Bar(constraintFactory())).Returns(1)));

            "Then the call configuration throws a FakeConfigurationException"
            .x(() => exception.Should().BeAnExceptionOfType <FakeConfigurationException>());

            "And the exception indicates that too many constraints were specified"
            .x(() => exception.Message.Should()
               .Be("Too many argument constraints specified. First superfluous constraint is <i => (i Is Object)>."));
        }
Ejemplo n.º 3
0
        public static void PassingHiddenConstraintToAMethod(
            IHaveAnObjectParameter fake,
            Func <object> constraintFactory,
            int result1,
            int result2)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IHaveAnObjectParameter>());

            "And a delegate that produces a constraint"
            .x(() => constraintFactory = () => A <object> .That.Matches(i => i == fake));

            "And I try to configure a method of the fake with this delegate"
            .x(() => A.CallTo(() => fake.Bar(constraintFactory())).Returns(1));

            "When I call the method with a matching argument"
            .x(() => result1 = fake.Bar(fake));

            "And I call the method with a non-matching argument"
            .x(() => result2 = fake.Bar(new object()));

            "Then the first result has the configured value"
            .x(() => result1.Should().Be(1));

            "Then the second result has the default value"
            .x(() => result2.Should().Be(0));
        }
Ejemplo n.º 4
0
        public static void TwoArgumentConstraints(
            IHaveAnObjectParameter fake,
            Exception exception)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IHaveAnObjectParameter>());

            "When I try to configure a method of the fake using two constraints on an argument"
            .x(() => exception = Record.Exception(() => A.CallTo(() =>
                                                                 fake.Bar(A <int> .That.Matches(i => i % 2 == 0) + A <int> .That.Matches(i => i % 2 == 1))).Returns(1)));

            "Then the call configuration throws an InvalidOperationException"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());

            "And the exception indicates that a nested argument constraint was used"
            .x(() => exception.Message.Should()
               .Be("An argument constraint, such as That, Ignored, or _, cannot be nested in an argument."));
        }
Ejemplo n.º 5
0
        public static void PassingIgnoredConstraintOfDerivedTypeToAMethod(
            IHaveAnObjectParameter fake,
            Exception exception,
            int result)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IHaveAnObjectParameter>());

            "When I try to configure a method of the fake with an Ignored constraint of a subclass of the parameter's type"
            .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Bar(A <Dummy> .Ignored)).Returns(42)));

            "And I call the method with an argument of the constraint's type"
            .x(() => result = fake.Bar(new Dummy()));

            "Then the call configuration doesn't throw an exception"
            .x(() => exception.Should().BeNull());

            "And the call is matched"
            .x(() => result.Should().Be(42));
        }