Silvermurk
Silvermurk

Reputation: 49

Moq returning zero results

I`m trying to use Moq to test EntityFramework code, and somehow it returns zero results after setup.

I tried to make a parameter-less constructor on workersController - same zero items returned

public class WorkerDbTest
{
    private readonly Mock<IWorkerReprisatory> _repo;
    private readonly WorkersController _controller;
    private readonly List<Worker> workers;
    public WorkerDbTest()
    {
        _logger = new Mock<ILogger<WorkersController>>();
        workers = new List<Worker>
        {
            new Worker() {
                Id = 0,
                FirstName = "John",
                MiddleName = "Abraham",
                LastName = "Doe",
                Workplace = "Bomj",
                BirthDate = new DateTime(1700,10,10),
                Employed = new DateTime(1800,10,10)},
            new Worker() {
                Id = 1,
                FirstName = "Alaster",
                MiddleName = "Crowly",
                LastName = "Johns",
                Workplace = "VipBomj",
                BirthDate = new DateTime(1800,12,12),
                Employed = new DateTime(1900,12,12)},
            new Worker() {
                Id = 2,
                FirstName = "Jane",
                MiddleName = "Susan",
                LastName = "Black",
                Workplace = "FemenistBobj",
                BirthDate = new DateTime(2000,11,11),
                Employed = new DateTime(2010,11,11)}
        };
        IQueryable<Worker> workersq = workers.AsQueryable();
        _repo = new Mock<IWorkerReprisatory>();
        _repo.Setup(x => x.GetAll(new WorkerQueryParameters())).Returns(workersq);
    }

    [Fact]
    public void GetOkResult()
    {
        var tst = _repo.Object.GetAll(new WorkerQueryParameters());
    }
}

//Controller class GetAllWorkers
public IActionResult GetAllWorkers(WorkerQueryParameters workerQueryParameters)
    {
        //_logger.LogInformation("GetAllCustomersStarted");
        var allWorkers = _workerRepository.GetAll(workerQueryParameters).ToList();
        var allWorkersDTO = allWorkers.Select(x => Mapper.Map<WorkerDTO>(x));
        if(Response != null)
        {
            Response.Headers.Add("X-Pagination",
            JsonConvert.SerializeObject(new { totalCount = _workerRepository.Count() }));
        }
        return Ok(allWorkersDTO);
    }

At the current time allWorkers object results 0 entries after being called. So I wanted to test out if I have any result from Mock object which then got var tst and there are still no entries.

Suppose that GetAll() Should return me those three guys I added there as iQueryables

Upvotes: 1

Views: 501

Answers (2)

Silvermurk
Silvermurk

Reputation: 49

Somewhy adding a param at startup and using it instead of New WorkerQueryParams() did the trick. I still have no idea why, but it works perfectly fine, and returns 3 entries. Thanks to everyone envolved and @Nikosi for his idea)

_params= new WorkerQueryParameters()
_repo.Setup(x => x.GetAll(_params)).Returns(workersq);
IQueryable<Worker> tst = _repo.Object.GetAll(_params);
var OkResponse = _controller.GetAllWorkers(_params);

Upvotes: 0

Kevin Smith
Kevin Smith

Reputation: 14436

Your setup method is matching on the an actual object refrence of WorkerQueryParameters, you'll need to use the same object refrence or, you'll need to match a given value with It.Is<WorkerQueryParameters>(x => x.Value == true) or just match any It.Any<WorkerQueryParameters>(), for example:

    _repo.Setup(x => x.GetAll(It.Any<WorkerQueryParameters>())).Returns(workersq);

It might also be worth setting your mock to be strick so that it throws instead of just returning null or default.

    _repo = new Mock<IWorkerReprisatory>(MockBehavior.Strict);

Upvotes: 0

Related Questions