コード例 #1
0
        public async Task OnException_WhenHandledSet_ExpectIsAvailableAndReturned()
        {
            var builder = new MockTypedBuilderFactory().Create()
                          .WithNextResponse(HttpStatusCode.InternalServerError, "\"error\"")
                          .WithUri(MockUri);

            bool?actual1 = null;
            bool?actual2 = null;

            //act
            var actualFinal = await builder
                              .Advanced
                              .OnException(
                ctx =>
            {
                actual1 = ctx.ExceptionHandled;
                ctx.ExceptionHandled = true;
            })
                              .OnException(
                ctx =>
            {
                actual2 = ctx.ExceptionHandled;
            })
                              .ResultAsync <string>();

            actual1.ShouldNotBeNull();
            actual1.Value.ShouldBeFalse();
            actual2.ShouldNotBeNull();
            actual2.Value.ShouldBeTrue();
            actualFinal.ShouldBeNull();
        }
コード例 #2
0
        public async Task OnResult_WhenResultSet_ExpectIsAvailableAndReturned()
        {
            var builder = new MockTypedBuilderFactory().Create()
                          .WithNextResponseOk("\"zero\"")
                          .WithUri(MockUri);

            string actual1 = null;
            string actual2 = null;

            //act
            var actualFinal = await builder
                              .Advanced
                              .OnResult <string>(ctx => ctx.Result = "one")
                              .OnResult <string>(
                ctx =>
            {
                actual1    = ctx.Result;
                ctx.Result = "two";
            })
                              .OnResult <string>(
                ctx =>
            {
                actual2    = ctx.Result;
                ctx.Result = "buckle my shoe";
            })
                              .ResultAsync <string>();

            actual1.ShouldBe("one");
            actual2.ShouldBe("two");
            actualFinal.ShouldBe("buckle my shoe");
        }
コード例 #3
0
        public async Task WhenContentAndSendingHandlerTypesMismatch_ExpectException()
        {
            var builder = new MockTypedBuilderFactory().Create().WithUri(MockUri);

            //act
            await builder.WithContent(TestResult.Default1()).AsPost().Advanced.OnSendingWithContent <Uri>(ctx => { }).ResultAsync <TestResult>();
        }
コード例 #4
0
        private static IMockTypedBuilder CreateBuilder()
        {
            var builder = new MockTypedBuilderFactory().Create();

            builder.Advanced.WithCaching(false);

            return(builder);
        }
コード例 #5
0
        public async Task WhenErrorAndErrorHandlerTypesMismatch_ExpectException()
        {
            var builder = new MockTypedBuilderFactory().Create().WithError(TestResult.Default1()).WithUri(MockUri);

            //act
            await Should.ThrowAsync <TypeMismatchException>(
                builder.WithErrorType <TestResult>().Advanced.OnError <Uri>(ctx => { var error = ctx.Error; }).ResultAsync <TestResult>());
        }
コード例 #6
0
        public async Task WhenResultAndSendingTypesMismatchAndSuppressTypeException_ExpectResult()
        {
            var builder = new MockTypedBuilderFactory().Create().WithResult(TestResult.Default1()).WithUri(MockUri);

            //act
            var actual = await builder.Advanced.WithSuppressTypeMismatchExceptions().OnSendingWithResult <Uri>(ctx => { }).ResultAsync <TestResult>();

            actual.ShouldNotBeNull();
        }
コード例 #7
0
        public async Task WhenDefaultResultAndResultTypesMismatch_ExpectException()
        {
            var builder = new MockTypedBuilderFactory().Create().WithError(TestResult.Default1()).WithUri(MockUri);

            //act
            await Should.ThrowAsync <TypeMismatchException>(
                builder.WithDefaultResult(TestResult.Default1())
                .Advanced.WithExceptionFactory(context => null)
                .ResultAsync <Uri>()
                );
        }
コード例 #8
0
        public async Task WhenDefaultResultAndResultTypesMismatchAndSuppressTypeException_ExpectNullResult()
        {
            var builder = new MockTypedBuilderFactory().Create().WithError(TestResult.Default1()).WithUri(MockUri);

            //act
            var actual = await builder.WithDefaultResult(TestResult.Default1())
                         .Advanced
                         .WithSuppressTypeMismatchExceptions()
                         .OnException(ctx => ctx.ExceptionHandled = true)
                         .ResultAsync <Uri>();

            actual.ShouldBeNull();
        }
コード例 #9
0
        public async Task WhenDependentUrisIsNull_ExpectNoException()
        {
            var builder =
                new MockTypedBuilderFactory().Create()
                .WithAllResponsesOk()
                .WithUri(MockUri)
                .Advanced;

            //act
            await builder
            .WithDependentUris(null)
            .SendAsync();
        }
コード例 #10
0
        public async Task WhenResultHandlerIsObjectType_ExpectSuccess()
        {
            var builder = new MockTypedBuilderFactory().Create().WithResult(TestResult.Default1()).WithUri(MockUri);

            //act
            var called = false;
            var result = await builder
                         .Advanced
                         .OnResult <object>(ctx => { called = true; })
                         .ResultAsync <TestResult>();

            result.ShouldNotBeNull();
            called.ShouldBeTrue();
        }
コード例 #11
0
        public async Task WhenHandlerIsSubTypeOfResult_ExpectSuccess()
        {
            var builder = new MockTypedBuilderFactory().Create().WithAllResponses(new MockHttpResponseMessage().WithContent(TestResult.SerializedDefault1)).WithUri(MockUri);

            //act
            var called = false;
            var result = await builder
                         .Advanced
                         .OnResult <TestResult>(ctx => { called = true; })
                         .ResultAsync <SubTestResult>();

            result.ShouldNotBeNull();
            called.ShouldBeTrue();
        }
コード例 #12
0
        public async Task WhenDependentUriIsNull_ExpectNoException()
        {
            using (var server = LocalWebServer.ListenInBackground(new XUnitMockLogger(_logger)))
            {
                var builder =
                    new MockTypedBuilderFactory().Create()
                    .WithAllResponsesOk()
                    .WithUri(server.ListeningUri)
                    .Advanced;

                //act
                await builder
                .WithDependentUri(null)
                .SendAsync();
            }
        }
コード例 #13
0
        public async Task WhenHandlerIsSuperTypeOfResult_ExpectException()
        {
            var builder = new MockTypedBuilderFactory().Create().WithResult(TestResult.Default1()).WithUri(MockUri);

            await Should.ThrowAsync <TypeMismatchException>(async() =>
            {
                var actual = await builder
                             .Advanced
                             .OnResult <SubTestResult>(ctx =>
                {
                    var result = ctx.Result;
                })
                             .ResultAsync <TestResult>();

                actual.ShouldBeNull();
            });
        }
コード例 #14
0
        public async Task WhenErrorTypeSetMultipleTimes_ExpectLastWins()
        {
            var builder = new MockTypedBuilderFactory().Create().WithError(TestResult.Default1()).WithUri(MockUri);

            //act
            Type       type   = null;
            TestResult error  = null;
            var        result = await builder
                                .WithErrorType <Uri>()
                                .WithErrorType <string>()
                                .WithErrorType <TestResult>()
                                .Advanced
                                .OnError <object>(ctx =>
            {
                ctx.ErrorHandled = true;
                type             = ctx.ErrorType;
                error            = ctx.Error as TestResult;
            })
                                .ResultAsync <TestResult>();

            error.ShouldNotBeNull();
            result.ShouldBeNull();
            typeof(TestResult).ShouldBe(type);
        }