0

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.

  • In each text mock the dependencies and inject them into the subject under test. Next invoke the member under test (ie: Handle) with the necessary arguments and finally assert/verify the expected behavior. – Nkosi Oct 10 '22 at 10:11

1 Answers1

0

it doesn't matter design patterns. you need to learn -What is unit testing_ -Which framework you should use? You should start with https://xunit.net/docs/getting-started/netfx/visual-studio