I'm running unit tests on my controllers and I often need to Action
return the navigation properties along with the template in question.
Example
public async Task<ActionResult> AlterarValores(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var autorizacao = await _db.Autorizacoes
.Include("Procedimentos")
.FirstOrDefaultAsync(a => a.AutorizacaoId == id);
if (autorizacao == null)
{
return HttpNotFound();
}
var model = autorizacao.Procedimentos
.Select(a => new AutorizacaoValoreViewModel
{
AutorizacaoId = a.AutorizacaoId,
Procedimento = a.DescricaoProcedimento,
});
return View(model);
}
I'm setting Mock
data = GetData().AsQueryable();
mockSet = new Mock<DbSet<Autorizacao>>();
mockSet.As<IDbAsyncEnumerable<Autorizacao>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<Autorizacao>(data.GetEnumerator()));
mockSet.As<IQueryable<Autorizacao>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<Autorizacao>(data.Provider));
mockSet.As<IQueryable<Autorizacao>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Autorizacao>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.Setup(a => a.Include("Procedimentos")).Returns(mockSet.Object);
mockContext = new Mock<SynsLegadoContext>();
mockContext.Setup(b => b.Autorizacoes).Returns(mockSet.Object);
Using .Include("Procedimentos")
works, but I would like to use .Include(a => a.Procedimentos)
but when I change the configuration of Mock
( mockSet.Setup(a => a.Include(b => b.Procedimentos)).Returns(mockSet.Object);
) I get the following error:
Message: Initialization method Syns.Web.Test.Areas.Counts.Controllers.AuthorizationsControllerTests.Initialize threw exception. System.NotSupportedException: System.NotSupportedException: Invalid setup on an extension method: a = > a.Include> (b => b.Procedures).
Would this be a limitation of the library? Or would you have some way around this setting and be able to use .Include(b => b.Procedimentos)