Сценарии - Простой moq для MediatR command 

Page / - Изменено Alexandr Fokin в 2024/01/04 19:19 

Простой moq для MediatR command 
Изменено Alexandr Fokin
в 2024/01/04 19:19

Оглавление

Moq билдер для Тестирование.

using MediatR;

using Moq;

namespace test.Model
{
   internal class MediatorMoqBuilder
    {
       private readonly Mock<IMediator> _mock = new Mock<IMediator>();

       public MediatorMoqBuilder AddCommand<TParameter, TResult>(
            Func<TParameter, CancellationToken, Task<TResult>> commandActionAsync
            )
           where TParameter : IRequest<TResult>
        {
            _mock
                .Setup(
                    e => e.Send(It.IsAny<TParameter>(), It.IsAny<CancellationToken>())
                )
                .Returns(commandActionAsync);
           return this;
        }

       public IMediator GetMediator()
        {
           return _mock.Object;
        }
    }
}