Exemple #1
0
    public async Task Handle_Loggerが取得できなかった場合は処理が実行されずに終了する()
    {
        var logger  = new TestLogger <LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse> >();
        var logging = new LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse>(new ServiceFactory(_ => null));

        var executed = false;
        await logging.Handle(new TestBehaviorRequest(), new CancellationToken(), () =>
        {
            executed = true;
            return(Task.FromResult(new TestBehaviorResponse()));
        });

        executed.Should().BeTrue();
    }
        public static void SetupCommand <TRequest, TResponse>(this Mock <IMediator> mockMediator, Func <TRequest, CancellationToken, Task <TResponse> > next, IValidator <TRequest> validator)
            where TRequest : ICommand <TResponse>
        {
            ValidatorBehavior <TRequest, TResponse> validatorBehavior = GetValidatorBehavior <TRequest, TResponse>(validator);

            var behaviorLoggerMock = new Mock <ILogger <LoggingBehavior <TRequest, TResponse> > >();
            var loggerBehavior     = new LoggingBehavior <TRequest, TResponse>(behaviorLoggerMock.Object);

            mockMediator.Setup(m => m.Send(It.IsAny <TRequest>(), default))
            .Returns <TRequest, CancellationToken>(async(request, cancellationToken) =>
            {
                return(await validatorBehavior.Handle(request, cancellationToken,
                                                      async() => await loggerBehavior.Handle(request, cancellationToken, async() => await next(request, cancellationToken))));
            });
        }
Exemple #3
0
    public async Task Handle_InOutValueCollectorが設定されている場合は値も出力される()
    {
        var logger = new TestLogger <LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse> >();

        var logging = new LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse>(
            new ServiceFactory(type =>
        {
            return(type.GenericTypeArguments[0].GetGenericTypeDefinition() == typeof(ILogger <>) ?
                   new List <ILogger <LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse> > >()
            {
                (ILogger <LoggingBehavior <TestBehaviorRequest, TestBehaviorResponse> >)logger
            } :
                   new List <ILoggingInOutValueCollector <TestBehaviorRequest, TestBehaviorResponse> >()
            {
                new TestLoggingInOutValueCollector1()
            });
        }));

        await logging.Handle(new TestBehaviorRequest()
        {
            Value = "A"
        }, new CancellationToken(), () =>
        {
            logger.Logs.Should().HaveCount(1);
            var logInfo = logger.Logs[0];
            var input   = logInfo.State.First(s => s.Key == "Input").Value.Should().Be("A");

            return(Task.FromResult(new TestBehaviorResponse()
            {
                Value = "B"
            }));
        });

        logger.Logs.Should().HaveCount(2);
        var logInfo = logger.Logs[1];

        var output = logInfo.State.First(s => s.Key == "Output").Value.Should().Be("B");
    }