Esempio n. 1
0
        public async Task when_handling_the_query_via_Post()
        {
            QueryName = "FakeQuery";
            FakeQueryProcessor.Setup(x => x.GetQueryType(QueryName)).Returns(typeof(FakeQuery));

            async Task should_return_the_result_from_the_query_processor()
            {
                var expected = new FakeResult();

                FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Returns(Task.FromResult(expected));

                var result = await Subject.HandlePost(QueryName, Json) as OkNegotiatedContentResult <object>;

                (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200);
                result.Content.Should().Be(expected);
            }

            async Task should_handle_QueryProcessorException()
            {
                FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryProcessorException("fail"));

                var result = await Subject.HandlePost(QueryName, Json);

                await result.ShouldBeErrorAsync("fail", 400);
            }

            async Task should_handle_QueryException()
            {
                FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new QueryException("invalid"));

                var result = await Subject.HandlePost(QueryName, Json);

                await result.ShouldBeErrorAsync("invalid", 400);
            }

            async Task should_handle_Exception()
            {
                FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail"));

                var result = await Subject.HandlePost(QueryName, Json);

                await result.ShouldBeErrorAsync("fail", 500);
            }

            async Task should_log_errors()
            {
                var fakeTraceWriter = new Mock <ITraceWriter>();
                var subject         = new FakeQueryController(FakeQueryProcessor.Object, fakeTraceWriter.Object)
                {
                    Request       = new HttpRequestMessage(),
                    Configuration = new HttpConfiguration()
                };

                FakeQueryProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeQuery>())).Throws(new Exception("fail"));

                await subject.HandlePost(QueryName, Json);

                fakeTraceWriter.Verify(x => x.Trace(It.IsAny <HttpRequestMessage>(), "UnhandledQueryException", TraceLevel.Error, It.IsAny <Action <TraceRecord> >()));
            }
        }
 public void SetUp()
 {
     FakeQueryProcessor = new Mock <IQueryProcessor>();
     Subject            = new FakeQueryController(FakeQueryProcessor.Object)
     {
         Request       = new HttpRequestMessage(),
         Configuration = new HttpConfiguration()
     };
 }