Moq function in controller test

Asked

Viewed 105 times

0

After a few hours trying, I arrived at this test code, but still I could not understand well the function of the Mock, and without it works the same way.

Note. The intention is to learn TDD with Moq...

---- Controller

private List<Compras> _comprasList;


public ComprasController(List<Compras> comprasList)
{
    _comprasList = comprasList;
}

[HttpGet]
public ActionResult GetAll()
{
    return _comprasList.ToList();
}

--- Test

private ComprasController _controller;
private Mock<List<Compras>> _listCompras;

public TestControllerCompras()
{
    _listCompras= new Mock<List<Compras>>();
    _controller = new ComprasController (_listCompras.Object);
}


[Fact]
public void Test_Get()
{
    //Arrang
    var compras = new List<Compras>
    {
        new Compras(){Id = 1, Produto = "produto1"},
        new Compras(){Id = 2, Produto = "produto2"},
        new Compras(){Id = 3, Produto = "produto3"},
        new Compras(){Id = 4, Produto = "produto4"}
    };

    _listCompras.Object.AddRange(compras);
    //Act
    var resul = _controller.GetAll();

    //Assert
    Assert.IsAssignableFrom<ActionResult<List<Compras>>>(resul);

}
  • If you wrote the test after the actual code you are not learning TDD. Of what Mock is talking?

  • The only thing I created was a class and controller just with Get msm. I’m using Moq4. The test would be even before the class and Controller?

  • I’m not sure what you’re trying to do, but it doesn’t look like you need to mock something in there.

  • I think I did something wrong so.... If you want to close the question, no problem... I will try to understand the tests before entering Moq.

2 answers

0

The Moq framework aims to simulate the database so you don’t necessarily need access to the database to test your business rules. Therefore, it does not make much sense to use Moq in this list. What you can do is create a Shopping repository and use dependency injection in Moq, so you can inject the Shopping repository dependency and simulate data to test your service. It would look something like this:

[TestClass]
public class ComprasServiceTest()
{
    private ComprasService GetService()
    {
        var service = new ComprasService();
        service.IComprasRepository = new Mock<IComprasRepository>().Objetct();
    }
}


[Fact]
public void Test_Get()
{
    
    var service = GetService();
    
    var comprasList = new List<Compras>()
    {
        new Compras { Id = 1, Produto =  "Produto" },
        new Compras { Id = 2, Produto =  "Produto2"}
    };
    
    //Arrange
    var mockComprasRepo = new Mock<IComprasRepository>();
    mockComprasRepo.Setup(x=> x.ListAll()).Returns(comprasList)

service.IComprasRepository= new Lazy<IComprasRepository>(() => mockComprasRepo .Object);
    
    //Act
    var result = service.SeuMetodoTeste();
    
    // Assert
    Assert.IsNotNull(result);

}

Above is a very simple example for you to understand a little bit how to perform your test and use Moq, you simulate the data with Moq so your service method is tested and call the repository, that would be the concept of layers. You test the business rules of your application that stays in the service and call the repository.

Another addendum is, I use the unit tests a lot to test my services the business rules, even for you to test your controller, I confess that I never tested but I know it is a little laborious, but there goes much of your test purpose.

You can look at this test project https://github.com/JessicaNathany/unit-test to have as a reference and understand the functioning of the Moq and the unit tests, I think it can help. :)

-1

I didn’t understand why to return to _comprasList with . Tolist() if it is already a list, and there is no result action as well.

When you mock something, you’re saying what it represents without actually entering the object, we usually use Mocks to avoid entering methods of Repository for example, but this also works with a service and the like.

I don’t see much point in mocking this list, but you can make a _buyList.Setup() and explore the options.

Browser other questions tagged

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