1

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
    ...
}
2
  • Are each one of those expressions going to be called in the method under test in the same use case? Commented Dec 8, 2016 at 19:03
  • @Nkosi Yes... I edit my question but i would be nice to know if there is a way to get it working Commented Dec 8, 2016 at 19:06

1 Answer 1

1

What is the return value of the RequestRepository.Get method? You can try to use SetupSequence and choose path you want to test. Something like:

 _requestGenericRepo.SetupSequence(m => m.Get(It.IsAny<Expression<Func<Request, bool>>>())
    .Returns(/*First call*/)
    .Returns(/*Second call*/)
    .Returns(/*Third call*/);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.