Esempio n. 1
0
        public async Task ShouldLogError()
        {
            var requestLogger = new UnhandledExceptionBehaviour <CreateArticleCommand, CreateArticleCommandHandler>(_logger.Object);

            try
            {
                await requestLogger.Handle(new CreateArticleCommand
                {
                    Id       = Guid.NewGuid(),
                    Title    = "Article Title",
                    Abstract = "Article Abstract",
                    Image    = null,
                    Content  = "Article Content"
                },
                                           new CancellationToken(), _response.Object);
            }
            catch (Exception)
            {
                _logger.Verify(
                    l => l.Log(
                        LogLevel.Error,
                        It.IsAny <EventId>(),
                        It.Is <It.IsAnyType>((v, t) => true),
                        It.IsAny <Exception>(),
                        It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)),
                    Times.Once
                    );
            }
        }
Esempio n. 2
0
        public async Task ShouldCallGetUserNameAsyncNeverIfAuthenticated()
        {
            var performanceBehaviour = new UnhandledExceptionBehaviour <CreateCalendarCommand, long>(_logger.Object);
            await performanceBehaviour.Handle(new CreateCalendarCommand { }, new CancellationToken(), () =>
            {
                return(Task.FromResult(1L));
            });

            _logger.Verify(i => i.Log(
                               LogLevel.Error,
                               It.IsAny <EventId>(),
                               It.Is <It.IsAnyType>((v, t) => true),
                               It.IsAny <Exception>(),
                               It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)), Times.Never);
        }
Esempio n. 3
0
        public async Task DoesUnhandledExceptionBehaviourWorksCorrectlyWhenNoExceptionIsThrown()
        {
            var loggerFactory = TestLoggerFactory.Create();
            var behaviour     = new UnhandledExceptionBehaviour <SampleRequest, SampleResponse>(loggerFactory.CreateLogger <SampleRequest>());
            var request       = new SampleRequest
            {
                Version   = "v2.0",
                IsWorking = true
            };

            await behaviour.Handle(request, CancellationToken.None,
                                   () => Task.FromResult(new SampleResponse {
                HasWorked = true
            }));

            loggerFactory.Sink.LogEntries.Should().BeEmpty();
        }
Esempio n. 4
0
        public void ShouldNotCallGetUserNameAsyncOnceIfUnauthenticated()
        {
            var performanceBehaviour = new UnhandledExceptionBehaviour <CreateCalendarCommand, long>(_logger.Object);

            Assert.CatchAsync <Exception>(async() =>
            {
                await performanceBehaviour.Handle(new CreateCalendarCommand {
                }, new CancellationToken(), () =>
                {
                    throw new Exception("Unhandled exception");
                });
            });
            _logger.Verify(i => i.Log(
                               LogLevel.Error,
                               It.IsAny <EventId>(),
                               It.Is <It.IsAnyType>((v, t) => true),
                               It.IsAny <Exception>(),
                               It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)), Times.Once);
        }
Esempio n. 5
0
        public Task DoesUnhandledExceptionBehaviourWorksCorrectlyWhenExceptionIsThrown()
        {
            var loggerFactory = TestLoggerFactory.Create();
            var behaviour     = new UnhandledExceptionBehaviour <SampleRequest, SampleResponse>(loggerFactory.CreateLogger <SampleRequest>());
            var request       = new SampleRequest
            {
                Version   = "v2.0",
                IsWorking = true
            };

            Action a = () => behaviour.Handle(request, CancellationToken.None,
                                              () => throw new Exception("Test Exception")).GetAwaiter().GetResult();

            a.Should().Throw <Exception>();
            loggerFactory.Sink.LogEntries.Should().ContainSingle(x =>
                                                                 x.LogLevel == LogLevel.Error && x.Message ==
                                                                 "eru Request: Unhandled Exception for Request SampleRequest {Version = v2.0, IsWorking = True}");
            return(Task.CompletedTask);
        }