I have something like this to test:
public void example(ModelView modelView)
{
//...
var statustId= 1;
var requests = _unitOfWork.RequestRepository.Get(m => m.RequestStatusId == statusId);
var requests = _unitOfWork.RequestRepository.Get(m => m.RequestTypeId == modelView.RequestTypeId);
var oldRequests = _unitOfWork.RequestRepository.Get(m => m.Created == DateTime.Now.AddDays(-7));
//...
}
How do i mock all the "GET" with different data?
This is what i got so far: not sure what the expression should look like since it is using a variables from the function.
[Test]
public void Test()
{
//Arrange
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(First GET - using local variable);
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(Second GET - using variable from modelView));
_requestGenericRepo.Setup(m => m.Get(
It.Is<Expression<Func<Request, bool>>>(e => e.Equals(Third GET));
//Act
var response = _controller.Example( new ModelView
{
RequestTypeId= 1
});
//Assert
...
}
-------------------EDIT: -------------------------------------------
It would be nice to know how to do in the above way but I can change all Get( To Get().Where like this:
public void example(ModelView modelView)
{
//...
var statustId= 1;
var requests = _unitOfWork.RequestRepository.Get().Where(m => m.RequestStatusId == statusId);
var requests = _unitOfWork.RequestRepository.Get().Where(m => m.RequestTypeId == modelView.RequestTypeId);
var oldRequests = _unitOfWork.RequestRepository.Get().Where(m => m.Created == DateTime.Now.AddDays(-7));
//...
}
[Test]
public void Test()
{
//Arrange
_requestGenericRepo.Setup(m => m.Get(
It.IsAny<Expression<Func<Request, bool>>>());
//Act
var response = _controller.Example( new ModelView
{
RequestTypeId= 1
});
//Assert
...
}