mercredi 20 mars 2019

C# How Test CommandsHandlers using Mediator e CQRS paterns

I never worked before with TDD before I staterd with some courses but I have no idea how do it in the real Life.

I have this project is MVC + DDD + Domain Notifications + CQRS + Event Sourcing + Unity of Work and Repository patterns and I would like to test but I have no idea where to start or even how to Mock Those injections.

There is my controller

public class DepartmentsController : BaseController
    {
        private readonly IDepartmentAppService _departmentAppService;


        public DepartmentsController(IDepartmentAppService departmentAppService,
            INotificationHandler<DomainNotification> notifications) : base(notifications)
        {
            _departmentAppService = departmentAppService;
        }

        public ActionResult Create(DepartmentViewModel departmentViewModel)
        {
            if (!ModelState.IsValid) return View(departmentViewModel);
            _departmentAppService.CreateDepartment(departmentViewModel);

            if (IsValidOperation())
            {
                this.AddToastMessage("Success", "Department Saved", ToastType.Success);
                return RedirectToAction("Index");
            }

            this.AddToastMessage("Error", GetNotifications().FirstOrDefault()?.Value, ToastType.Error);
            return View(departmentViewModel);
        }
    }

And my serivce Layer

 private readonly IMediatorHandler _bus;
    private readonly IDepartmentRepository _departmentRepository;
    private readonly IMapper _mapper;


    public DepartmentAppService(IMapper mapper,
        IDepartmentRepository departmentRepository,
        IMediatorHandler bus)
    {
        _departmentRepository = departmentRepository;
        _bus = bus;
        _mapper = mapper;
    }



     public void CreateDepartment(DepartmentViewModel departmentViewModel)
    {
        var registerCommand = _mapper.Map<RegisterDepartmentCommand>(departmentViewModel);
        _bus.SendCommand(registerCommand);
    }

I create this simple test method but I really don't know what expect, I'm not sure what should I do at this point, could you guys give ma hand here?

[TestClass()]
public class DepartmentAppServiceTests
{

    private readonly Mock<IMediatorHandler> _mediatorHandlerMock;
    private readonly Mock<IDepartmentRepository> _departmentRepositoryMock;
    private readonly Mock<IMapper> _mapper;

    public DepartmentAppServiceTests()
    {
        _mediatorHandlerMock = new Mock<IMediatorHandler>();
        _departmentRepositoryMock = new Mock<IDepartmentRepository>();
        _mapper = new Mock<IMapper>();

    }

    [TestMethod()]
    public void CreateDepartmentTest()
    {
        //Arrange
        var fakeDepartment = GetFakeDeparment();
        var fakeDepartmentVm = new DepartmentViewModel();

        Mock<RegisterDepartmentCommand> expected = new Mock<RegisterDepartmentCommand>();
        expected.SetupGet(x => x.Id).Returns(new Guid());
        expected.SetupGet(x => x.Name).Returns("Department Test");

        _departmentRepositoryMock.Setup(x => x.GetById(It.IsAny<Guid>()))
            .Returns(fakeDepartment);

        _mediatorHandlerMock.Setup(x => x.SendCommand(It.IsAny<RegisterDepartmentCommand>()));

        //Act
        var departmentService = new DepartmentAppService(
            _mapper.Object,
            _departmentRepositoryMock.Object,
            _mediatorHandlerMock.Object);


        _mapper.Setup(m => m.Map<Department, DepartmentViewModel>(It.IsAny<Department>())).Returns(fakeDepartmentVm);

        departmentService.CreateDepartment(fakeDepartmentVm);

        //Assert
      //  !_notifications.HasNotifications();



    }

    private Department GetFakeDeparment()
    {
        return new Department()
        {
            Id = new Guid(),
            Name = "fakeName",

        };
    }
}

}

Aucun commentaire:

Enregistrer un commentaire