I need to know how to Unit testing in CQRS pattern. as a beginer i dont have much idea about that. I develope below Command and queries. I need to Test those
Here is my add student command
public record AddStudentCommand(Student student):IRequest<Student>;
Hear is my Add Studnet Hadler
public class AddStudentHandler : IRequestHandler<AddStudentCommand, Student>
{
private readonly appDbContext _context;
public AddStudentHandler(appDbContext context)
{
_context = context;
}
public async Task<Student> Handle(AddStudentCommand request, CancellationToken cancellationToken)
{
_context.students.Add(request.student);
await _context.SaveChangesAsync();
return request.student;
}
}
This is Get All Student Query
public record GetAllStudentDataQuery : IRequest<IEnumerable<Student>>;
This is my Get All Student Handler
public class GetAllStudentHandler : IRequestHandler<GetAllStudentDataQuery, IEnumerable<Student>>
{
private readonly appDbContext _context;
public GetAllStudentHandler(appDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Student>> Handle(GetAllStudentDataQuery request, CancellationToken cancellationToken)
{
return _context.students;
}
}
I need to Unit test these methods.