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?– Maniero
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?
– Carlos
I’m not sure what you’re trying to do, but it doesn’t look like you need to mock something in there.
– Maniero
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.
– Carlos