public static void MultipleCallbacks(
            IFoo fake,
            bool firstWasCalled,
            bool secondWasCalled,
            int returnValue)
        {
            "Given a fake"
                .x(() => fake = A.Fake<IFoo>());

            "And I configure a method to invoke two actions and return a value"
                .x(() =>
                    A.CallTo(() => fake.Baz())
                        .Invokes(x => firstWasCalled = true)
                        .Invokes(x => secondWasCalled = true)
                        .Returns(10));

            "When I call the method"
                .x(() => returnValue = fake.Baz());

            "Then it calls the first callback"
                .x(() => firstWasCalled.Should().BeTrue());

            "And it calls the first callback"
                .x(() => secondWasCalled.Should().BeTrue());

            "And it returns the configured value"
                .x(() => returnValue.Should().Be(10));
        }
Exemple #2
0
        public static void MultipleCallbacks(
            IFoo fake,
            bool firstWasCalled,
            bool secondWasCalled,
            int returnValue)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IFoo>());

            "And I configure a method to invoke two actions and return a value"
            .x(() =>
               A.CallTo(() => fake.Baz())
               .Invokes(x => firstWasCalled  = true)
               .Invokes(x => secondWasCalled = true)
               .Returns(10));

            "When I call the method"
            .x(() => returnValue = fake.Baz());

            "Then it calls the first callback"
            .x(() => firstWasCalled.Should().BeTrue());

            "And it calls the first callback"
            .x(() => secondWasCalled.Should().BeTrue());

            "And it returns the configured value"
            .x(() => returnValue.Should().Be(10));
        }
        public static void MultipleCallbacks(
            IFoo fake,
            bool firstWasCalled,
            bool secondWasCalled,
            int returnValue)
        {
            "establish"
                .x(() => fake = A.Fake<IFoo>());

            "when configuring multiple callback"
                .x(() =>
                    {
                        A.CallTo(() => fake.Baz())
                            .Invokes(x => firstWasCalled = true)
                            .Invokes(x => secondWasCalled = true)
                            .Returns(10);

                        returnValue = fake.Baz();
                    });

            "it should call the first callback"
                .x(() => firstWasCalled.Should().BeTrue());

            "it should call the second callback"
                .x(() => secondWasCalled.Should().BeTrue());

            "it should return the configured value"
                .x(() => returnValue.Should().Be(10));
        }
        public void MethodsWork()
        {
            IConfigurationRoot configuration = GetConfig();

            IFoo foo = configuration.GetSection("foo").CreateReloadingProxy <IFoo>();

            Assert.Equal(123 * 2, foo.Baz());

            ChangeConfig(configuration);

            Assert.Equal(456 * 2, foo.Baz());
        }
        public static void InvokesAfterStrictValueTypeKeepsDefaultReturnValue(
            IFoo fake,
            int result)
        {
            "Given a strict fake"
            .x(() => fake = A.Fake <IFoo>(options => options.Strict()));

            "And I configure a value type method to invoke an action"
            .x(() => A.CallTo(() => fake.Baz()).Invokes(() => { }));

            "When I call the method"
            .x(() => result = fake.Baz());

            "Then it returns the same value as an unconfigured fake"
            .x(() => result.Should().Be(A.Fake <IFoo>().Baz()));
        }
        public static void UnusedNonVoidCallSpec(
            IFoo fake,
            Exception exception)
        {
            "Given a strict fake"
            .x(() => fake = A.Fake <IFoo>(o => o.Strict()));

            "When I specify a call to a void method without configuring its behavior"
            .x(() => A.CallTo(() => fake.Baz()));

            "And I make a call to that method"
            .x(() => exception = Record.Exception(() => fake.Baz()));

            "Then it throws an expectation exception"
            .x(() => exception.Should().BeAnExceptionOfType <ExpectationException>());
        }
        [Category("NotWorking")]          // it requires OneWay
        public void RequestBasedOnContract2()
        {
            CustomBinding b = CreateBinding(delegate(Message input) {
                return(null);
            }, true);

            IFoo foo = ChannelFactory <IFoo> .CreateChannel(b, CreateX509EndpointAddress ("stream:dummy"));

            foo.Baz("TEST");
        }
Exemple #8
0
        public static void WithVoidReturnType(IFoo fake, bool methodWasIntercepted)
        {
            "Given a fake with a void method"
            .x(() => fake = A.Fake <IFoo>());

            "And the fake is configured to set a flag for any void method"
            .x(() => A.CallTo(fake).WithVoidReturnType().Invokes(() => methodWasIntercepted = true));

            "When a void method is called on the fake"
            .x(() => fake.Baz());

            "Then the flag is set"
            .x(() => methodWasIntercepted.Should().BeTrue());
        }
        public static void DoesNothingAfterStrictValueTypeKeepsDefaultReturnValue(
            IFoo fake,
            int result)
        {
            "Given a strict fake"
            .x(() => fake = A.Fake <IFoo>(options => options.Strict()));

            "And I configure all methods to do nothing"
            .x(() => A.CallTo(fake).DoesNothing());

            "When I call a value type method"
            .x(() => result = fake.Baz());

            "Then it returns the same value as an unconfigured fake"
            .x(() => result.Should().Be(A.Fake <IFoo>().Baz()));
        }
Exemple #10
0
        public static void WithNonVoidReturnTypeAndVoidMethod(
            IFoo fake,
            bool methodWasIntercepted)
        {
            "Given a fake with a void method"
            .x(() => fake = A.Fake <IFoo>());

            "And the fake is configured to set a flag for any non-void return type"
            .x(() => A.CallTo(fake).WithNonVoidReturnType().Invokes(() => methodWasIntercepted = true));

            "When the void method is called"
            .x(() => fake.Baz());

            "Then the flag is not set"
            .x(() => methodWasIntercepted.Should().BeFalse());
        }
Exemple #11
0
        public static void MultipleThrows(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration <int> configuration,
            Exception exception)
        {
            "establish"
            .x(() => fake = A.Fake <IFoo>());

            "when configuring a return then a throw on the same configuration"
            .x(() =>
            {
                configuration = A.CallTo(() => fake.Baz());
                configuration.Throws(new ArgumentNullException());
                exception = Record.Exception(() => configuration.Throws(new ArgumentException()));
            });

            "it should throw an InvalidOperationException"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
Exemple #12
0
        public static void MultipleReturns(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration <int> configuration,
            Exception exception)
        {
            "establish"
            .x(() => fake = A.Fake <IFoo>());

            "when configuring multiple returns on the same configuration"
            .x(() =>
            {
                configuration = A.CallTo(() => fake.Baz());
                configuration.Returns(42);
                exception = Record.Exception(() => configuration.Returns(0));
            });

            "it should throw an invalid operation exception"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
Exemple #13
0
        public static void ReturnThenCallsBaseMethod(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration <int> configuration,
            Exception exception)
        {
            "establish"
            .x(() => fake = A.Fake <IFoo>());

            "when configuring a return then base method call on the same configuration"
            .x(() =>
            {
                configuration = A.CallTo(() => fake.Baz());
                configuration.Returns(42);
                exception = Record.Exception(() => configuration.CallsBaseMethod());
            });

            "it should throw an InvalidOperationException"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
        public static void MultipleThrows(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration <int> configuration,
            Exception exception)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IFoo>());

            "And I configure the return method to throw an exception"
            .x(() =>
            {
                configuration = A.CallTo(() => fake.Baz());
                configuration.Throws <ArgumentNullException>();
            });

            "When I use the same configuration object to have the method throw an exception again"
            .x(() => exception = Record.Exception(() => configuration.Throws <ArgumentException>()));

            "Then it throws an invalid operation exception"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
        public static void ReturnThenCallsBaseMethod(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration <int> configuration,
            Exception exception)
        {
            "Given a fake"
            .x(() => fake = A.Fake <IFoo>());

            "And I configure the return value for the method"
            .x(() =>
            {
                configuration = A.CallTo(() => fake.Baz());
                configuration.Returns(42);
            });

            "When I use the same configuration object to have the method call the base method"
            .x(() => exception = Record.Exception(() => configuration.CallsBaseMethod()));

            "Then it throws an invalid operation exception"
            .x(() => exception.Should().BeAnExceptionOfType <InvalidOperationException>());
        }
Exemple #16
0
        public void ExceptionInCustomArgumentValueFormatter(IFoo fake, Exception exception)
        {
            "Given a custom argument value formatter that throws an exception"
            .See <HasCustomValueFormatterValueFormatter>();

            "And a fake"
            .x(() => fake = A.Fake <IFoo>());

            "And no call is made to the fake"
            .x(() => { });

            "When an assertion using an argument of the type with the throwing formatter is made on the fake"
            .x(() => exception = Record.Exception(() => A.CallTo(() => fake.Baz(new HasCustomValueFormatter())).MustHaveHappened()));

            "Then a UserCallbackException should be thrown"
            .x(() => exception.Should().BeAnExceptionOfType <UserCallbackException>());

            "And its message should describe where the exception was thrown from"
            .x(() => exception.Message.Should().Be("Custom argument value formatter 'FakeItEasy.Specs.UserCallbackExceptionSpecs+HasCustomValueFormatterValueFormatter' threw an exception. See inner exception for details."));

            "And the inner exception should be the original exception"
            .x(() => exception.InnerException.Should().BeAnExceptionOfType <MyException>().Which.Message.Should().Be("Oops"));
        }
        public static void DoesNothingAfterStrictValueType(
            IFoo fake,
            int result)
        {
            "Given a strict fake"
                .x(() => fake = A.Fake<IFoo>(options => options.Strict()));

            "And I configure all methods to do nothing"
                .x(() => A.CallTo(fake).DoesNothing());

            "When I call a value type method"
                .x(() => result = fake.Baz());

            "Then it returns a default instance of the value type"
                .x(() => result.Should().Be(0));
        }
Exemple #18
0
        public void NonVoidMethodWithReturns(
            IFoo foo,
            int result1,
            int result2,
            int result3)
        {
            "Given a fake"
                .x(() => foo = A.Fake<IFoo>());

            "When a non-void method is configured to return 1 once then return 2"
                .x(() =>
                    A.CallTo(() => foo.Baz()).Returns(1).Once()
                        .Then.Returns(2));

            "And the method is called 3 times"
                .x(() =>
                {
                    result1 = foo.Baz();
                    result2 = foo.Baz();
                    result3 = foo.Baz();
                });

            "Then the first call returns 1"
                .x(() => result1.Should().Be(1));

            "And the second call returns 2"
                .x(() => result2.Should().Be(2));

            "And the third call returns 2"
                .x(() => result3.Should().Be(2));
        }
        public static void MultipleReturns(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration<int> configuration,
            Exception exception)
        {
            "establish"
                .x(() => fake = A.Fake<IFoo>());

            "when configuring multiple returns on the same configuration"
                .x(() =>
                {
                    configuration = A.CallTo(() => fake.Baz());
                    configuration.Returns(42);
                    exception = Record.Exception(() => configuration.Returns(0));
                });

            "it should throw an invalid operation exception"
                .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
        }
        public static void ReturnThenCallsBaseMethod(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration<int> configuration,
            Exception exception)
        {
            "establish"
                .x(() => fake = A.Fake<IFoo>());

            "when configuring a return then base method call on the same configuration"
                .x(() =>
                {
                    configuration = A.CallTo(() => fake.Baz());
                    configuration.Returns(42);
                    exception = Record.Exception(() => configuration.CallsBaseMethod());
                });

            "it should throw an InvalidOperationException"
                .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
        }
        public static void MultipleReturns(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration<int> configuration,
            Exception exception)
        {
            "Given a fake"
                .x(() => fake = A.Fake<IFoo>());

            "And I configure the return value for the method"
                .x(() =>
                {
                    configuration = A.CallTo(() => fake.Baz());
                    configuration.Returns(42);
                });

            "When I use the same configuration object to set the return value again"
                .x(() => exception = Record.Exception(() => configuration.Returns(0)));

            "Then it throws an invalid operation exception"
                .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
        }
        public static void UnusedNonVoidCallSpec(
            IFoo fake,
            Exception exception)
        {
            "Given a strict fake"
                .x(() => fake = A.Fake<IFoo>(o => o.Strict()));

            "When I specify a call to a void method without configuring its behavior"
                .x(() => A.CallTo(() => fake.Baz()));

            "And I make a call to that method"
                .x(() => exception = Record.Exception(() => fake.Baz()));

            "Then it throws an expectation exception"
                .x(() => exception.Should().BeAnExceptionOfType<ExpectationException>());
        }
        public static void InvokesAfterStrictValueType(
            IFoo fake,
            int result)
        {
            "Given a strict fake"
                .x(() => fake = A.Fake<IFoo>(options => options.Strict()));

            "And I configure a value type method to invoke an action"
                .x(() => A.CallTo(() => fake.Baz()).Invokes(() => { }));

            "When I call the method"
                .x(() => result = fake.Baz());

            "Then it returns a default instance of the value type"
                .x(() => result.Should().Be(0));
        }
        public static void MultipleThrows(
            IFoo fake,
            IReturnValueArgumentValidationConfiguration<int> configuration,
            Exception exception)
        {
            "establish"
                .x(() => fake = A.Fake<IFoo>());

            "when configuring a return then a throw on the same configuration"
                .x(() =>
                {
                    configuration = A.CallTo(() => fake.Baz());
                    configuration.Throws(new ArgumentNullException());
                    exception = Record.Exception(() => configuration.Throws(new ArgumentException()));
                });

            "it should throw an InvalidOperationException"
                .x(() => exception.Should().BeAnExceptionOfType<InvalidOperationException>());
        }