ABNERMATHEUS / UnitTest

A application trucks CRUD to apresentation about unit test in c#. Using clean architecture, CQRS and XUnit.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

English

Unit Testing in Trucks Solution

This solution uses unit tests to verify the correctness of the code. The tests are written using xUnit, a popular testing framework in .NET. The tests are organized into different classes, each focusing on a specific feature or functionality of the application.

Arrange, Act, Assert

Unit tests typically follow the Arrange-Act-Assert pattern:

  • Arrange: This is where you set up the conditions for your test. This includes creating objects, setting up mock behaviors, and arranging the system under test.

  • Act: This is where you invoke the method or function you want to test.

  • Assert: This is where you verify that the action of the method or function under test produced the expected results.

Examples

In the CreateTruckCommandHandlerUnitTest and DeleteTruckCommandHandlerUnitTest classes, the constructor is used to Arrange the necessary objects for the tests. This includes creating a mock repository and the command handler with its dependencies.

In the Handle_ValidRequest_ShouldReturnSuccessResult test, the Act phase is where the Handle method of the command handler is called with a valid request. The Assert phase then checks that the result of the Handle method is as expected.

In the Handle_InvalidRequest_ShouldReturnFailureResult test, multiple invalid requests are tested using the [Theory] and [InlineData] attributes. This allows for testing multiple scenarios with different input data.

Mocking

Mocking is used to isolate the unit of code under test. In this solution, the Moq library is used to create mock objects. The repository is mocked so that no actual database operations are performed during testing.

Running the Tests

To run the tests, you can use the Test Explorer in Visual Studio or run the dotnet test command in the terminal from the solution directory.

Further Reading

For more information on unit testing in .NET, you can refer to the official documentation.


MediatR and Unit Testing

MediatR is a simple library that allows for in-process messaging. It supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via CQRS.

In the context of unit testing, MediatR can be very beneficial. It allows you to isolate your handlers and test them in isolation, without needing to worry about their external dependencies. This is because MediatR decouples the in-process sending of messages from handling messages.

In the Trucks solution, MediatR is used to send commands like CreateTruckCommand and DeleteTruckCommand to their respective handlers CreateTruckCommandHandler and DeleteTruckCommandHandler. These handlers can then be unit tested independently, ensuring that they behave correctly when a command is sent.

This makes your tests more focused and easier to manage, as you can test individual pieces of functionality in isolation. It also makes your code more maintainable and easier to understand, as each handler has a single responsibility.


Português

Testes Unitários na Solução Trucks

Esta solução usa testes unitários para verificar a correção do código. Os testes são escritos usando o xUnit, um framework de testes popular no .NET. Os testes são organizados em diferentes classes, cada uma focando em uma funcionalidade específica do aplicativo.

Arrange, Act, Assert

Os testes unitários geralmente seguem o padrão Arrange-Act-Assert:

  • Arrange: Aqui você configura as condições para o seu teste. Isso inclui criar objetos, configurar comportamentos de mock e organizar o sistema em teste.

  • Act: Aqui você invoca o método ou função que deseja testar.

  • Assert: Aqui você verifica se a ação do método ou função em teste produziu os resultados esperados.

Exemplos

Nas classes CreateTruckCommandHandlerUnitTest e DeleteTruckCommandHandlerUnitTest, o construtor é usado para organizar os objetos necessários para os testes. Isso inclui criar um repositório mock e o manipulador de comando com suas dependências.

No teste Handle_ValidRequest_ShouldReturnSuccessResult, a fase Act é onde o método Handle do manipulador de comando é chamado com uma solicitação válida. A fase Assert então verifica se o resultado do método Handle é o esperado.

No teste Handle_InvalidRequest_ShouldReturnFailureResult, várias solicitações inválidas são testadas usando os atributos [Theory] e [InlineData]. Isso permite testar vários cenários com diferentes dados de entrada.

Mocking

O Mocking é usado para isolar a unidade de código em teste. Nesta solução, a biblioteca Moq é usada para criar objetos mock. O repositório é simulado para que nenhuma operação de banco de dados real seja realizada durante o teste.

Executando os Testes

Para executar os testes, você pode usar o Test Explorer no Visual Studio ou executar o comando dotnet test no terminal a partir do diretório da solução.

CQRS e Testes Unitários

CQRS significa Command Query Responsibility Segregation. É um padrão de design que separa a leitura e a gravação em diferentes modelos. Isso permite que você escale e otimize independentemente suas cargas de trabalho de leitura e gravação e oferece a flexibilidade de modelar suas leituras e gravações para se ajustar melhor às necessidades do seu aplicativo.

No contexto de testes unitários, o CQRS pode ser benéfico, pois permite testes mais granulares. Como os comandos (gravações) e as consultas (leituras) são tratados separadamente, você pode escrever testes unitários que visam comandos ou consultas específicas. Isso pode tornar seus testes mais focados e fáceis de gerenciar.

MediatR e Testes Unitários

MediatR é uma biblioteca simples que permite a troca de mensagens no processo. Ela suporta solicitação/resposta, comandos, consultas, notificações e eventos, sincronos e assíncronos com despacho inteligente via CQRS.

No contexto de testes unitários, MediatR pode ser muito benéfico. Ele permite que você isole seus manipuladores e os teste isoladamente, sem precisar se preocupar com suas dependências externas. Isso ocorre porque o MediatR desacopla o envio de mensagens no processo do manuseio de mensagens.

Na solução Trucks, MediatR é usado para enviar comandos como CreateTruckCommand e DeleteTruckCommand para seus respectivos manipuladores CreateTruckCommandHandler e DeleteTruckCommandHandler. Esses manipuladores podem então ser testados unitariamente de forma independente, garantindo que eles se comportem corretamente quando um comando é enviado.

Isso torna seus testes mais focados e fáceis de gerenciar, pois você pode testar funcionalidades individuais isoladamente. Também torna seu código mais fácil de manter e entender, pois cada manipulador tem uma única responsabilidade.

About

A application trucks CRUD to apresentation about unit test in c#. Using clean architecture, CQRS and XUnit.


Languages

Language:C# 100.0%