Apply filter to a list returned by a mock

Asked

Viewed 31 times

0

Good night! I have the following scenario:

A concrete class called Usersservice, which receives by injection of dependencies the repository through an interface and a notifier. She gets like this

private readonly IUsuarioRepository _repository;

    public UsuarioService(INotificator notificador, IUsuarioRepository repository) : base(notificador, repository)
    {
        _repository = repository;
    }

Within this service I have a method called Buscaroperacoesusuario(Guid idUsuario), this method just calls a repository method with the same name. In the repository is where the logic of this query is.

What happens is that when I create a mock of my Usuarioservice, and configure in the setup what I hope to return from the Search method.

Here the method of my service

public async Task<List<Operacao>> BuscarOperacoesUsuario(Guid idUsuario)
    {
        return await _repository.BuscarOperacoesUsuario(idUsuario);
    }

Here the method in my repository

public async Task<List<Operacao>> BuscarOperacoesUsuario(Guid idUsuario)
    {
        return await Db.Operacoes.AsNoTracking()
            .Include(operacao => operacao.UsuariosOperacoes)
            .Where(operacao => operacao.UsuariosOperacoes.Any(uo => uo.IdUsuario == idUsuario))
            .ToListAsync();
    }

Here the mock creation and method setup

 var mocker = new AutoMocker();
 var usuarioService = mocker.CreateInstance<UsuarioService>();
mocker.GetMock<IUsuarioRepository>().Setup(c => c.BuscarOperacoesUsuario(It.IsAny<Guid>   ())).ReturnsAsync(operacoes);

In the case there in operations is a list with 4 items, and 2 of them have the same user ID, but the method always returns 4 items and not only the 2 of the user I am passing in the mock call

            var resultado = await usuarioService.BuscarOperacoesUsuario(usuarioValido.Id);

NOTE: I changed the method of my service to return a list without applying the filter, I saved this list in a variable and then I applied the filter and then it worked... but so I would always have to return EVERYTHING from the bank and apply the filter in memory and it won’t look good.

User service so it applies the WHERE in my list.

  public async Task<List<Operacao>> BuscarOperacoesUsuario(Guid idUsuario)
    {
        var teste = await _repository.BuscarOperacoesUsuario(idUsuario);
        return teste.Where(operacao => operacao.UsuariosOperacoes.Any(uo => uo.IdUsuario == idUsuario)).ToList();
    }

Repository

 public async Task<List<Operacao>> BuscarOperacoesUsuario(Guid idUsuario)
    {
        return await Db.Operacoes.AsNoTracking()
            .Include(operacao => operacao.UsuariosOperacoes)
            //.Where(operacao => operacao.UsuariosOperacoes.Any(uo => uo.IdUsuario == idUsuario))
            .ToListAsync();
    }

1 answer

1


This happens because the method you mock has no logic, it simply returns a list.

You should do something like this:

mocker.GetMock<IUsuarioRepository>()
   .Setup(c => c.BuscarOperacoesUsuario(It.IsAny<Guid>()))
   .ReturnsAsync((Guid usuarioId) => 
      operacoes.Where(op => 
         op.UsuariosOperacoes.Any(uo => uo.IdUsuario == usuarioId)));
  • I did it myself but then if my filter changes there in the repository my test will continue passing right? I tried to mock the pq repository that implements the method but I couldn’t because of the db context that I need to pass to the base class

  • @Rafaelscheffer I think you’re confusing things. If you’re testing the service, you need to consider that the repository will be right...

  • Aaaaaa I think I got it, I started studying unit tests now, in fact I’m going to have to have another test just for repository, beauty, thank you very much!

  • @Rafaelscheffer This. You will test each class in isolation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.