C# As Testart Commandshandlers using Mediator and CQRS standard

Asked

Viewed 179 times

1

I have never worked with tests before and would like to learn.

I’m using the MVC + DDD + Domain Notifications + CQRS + Event Sourcing + Unity of Work and Repository standards.

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 Service 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 had no idea where I should start to create my test projects, so I started in my application layer and created a method to test my commands, starting with Create. So I wrote this.

But like I said I have no idea what I’m doing, I’d like to know if I’m on the right track or should I do something else ...

[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",

        };
    }
}

}

  • 1

    a hint: try to express in the test method the following: what you are testing, what the arrange and what the expected result, for example: "Departament_criadeptovalido_mapped"

  • Thank you, I just haven’t put the name yet because I really don’t know what I should test here!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.