5
My controller:
[Authorize]
public class DominioController : Controller
{
private IDominioDB _db;
public DominioController(IDominioDB db)
{
_db = db;
}
// GET: Dominio
public async Task<ActionResult> Index()
{
var userID = User.Identity.GetUserId();
var d = await _db.Dominios.Where(x => x.idUsu == userID).ToListAsync();
ViewBag.Username = User.Identity.Name;
return View(d);
}
}
My method of testing:
[TestMethod]
public async Task VERIFICA_CONTROLE_DOMINIO()
{
var data = new List<DBDominio>()
{
new DBDominio() { idUsu = "usuario1" },
new DBDominio() { idUsu = "usuario2" },new DBDominio() { idUsu = "usuario2" },
new DBDominio() { idUsu = "usuario3" },new DBDominio() { idUsu = "usuario3" }
}.AsQueryable();
var mockSet = new Mock<DbSet<DBDominio>>();
mockSet.As<IDbAsyncEnumerable<DBDominio>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<DBDominio>(data.GetEnumerator()));
mockSet.As<IQueryable<DBDominio>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<DBDominio>(data.Provider));
mockSet.As<IQueryable<DBDominio>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<DBDominio>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<DBDominio>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var repo = new Mock<Interfaces.IDominioDB>();
repo.Setup(c => c.Dominios).Returns(mockSet.Object);
var context = new Mock<ControllerContext>();
var mockIdentity = new Mock<IIdentity>();
context.SetupGet(x => x.HttpContext.User.Identity).Returns(mockIdentity.Object);
mockIdentity.Setup(x => x.Name).Returns("usuario1");
// Arrange
DominioController controller = new DominioController(repo.Object)
{
ControllerContext = context.Object
};
// Act
ViewResult result = (ViewResult)await controller.Index();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(result.ViewBag.Username , "usuario1");
}
I want to know how I could check if the var d = await _db.Dominios.Where(x => x.idUsu == userID).ToListAsync();
that is within the controler returns the right amount of records, this is possible this way?
I know I could create a Service layer between the controller and EF, what I don’t like this solution is that in some time there are 200 different methods within a service and start giving me maintenance problems.
See if this helps. If it doesn’t help, I’ll write one more answer.
– Leonel Sanches da Silva
I liked your solution, but with it, how would you know how many records would return on my line?
var d = await _db.Dominios.Where(x => x.idUsu == userID).ToListAsync();
– Ricardo
It depends on the organization of your Mock. The advantage of my proposal in relation to Moq is that you effectively test with data, even if invented. Therefore, the counts are known to you.
– Leonel Sanches da Silva
So it’s this mock organization that made me ask the question :)
– Ricardo
If you like, I can expand that answer into a more specialized context for you. What do you think?
– Leonel Sanches da Silva
I think great, actually I already implemented your answer in the code, I just didn’t close the question because I still don’t understand how I could test that part of the code I put in the question, but my code is already using your solution.
– Ricardo
Okay, I’ll write you that tomorrow.
– Leonel Sanches da Silva