I have a method that modifies data in handler.
if (request.Active)
{
var consentsToDeactivate = await _context.Consents.Where(x => x.Id != consent.Id).ToListAsync();
consentsToDeactivate.ForEach(x => x.Active = false);
_context.Consents.UpdateRange(consentsToDeactivate);
}
I need to mock this to to get a list where Consents
are not equal to specific ID but I fail to do so. Here's what I've tried already:
var consentList = new List<Consent>()
{
new Consent{ Id = new Guid("9205d00d-b443-412c-b8ad-6dcddc140ddf"), Name ="Consent1", Active=true, CreatedDate = DateTime.Now },
new Consent{ Id = new Guid("6fa0ddb1-9867-49d5-b093-0f7eeb3fcbb6"),Name ="AConsent2", Active=false, CreatedDate = DateTime.Now },
}.AsQueryable();
var context = new Mock<IUnicornDbContext>();
var consentData = new Mock<DbSet<Consent>>();
consentData.Setup(x => x.FindAsync(It.IsAny<Guid>())).ReturnsAsync(new Consent() { Content = "some content", Name = "Test Consent", CreatedDate = DateTime.Now });
consentData.Setup(x => x.Where(It.IsAny<Expression<Func<Consent, bool>>>())).Returns(consentList);
context.Setup(x => x.Consents).Returns(consentData.Object);
But it fails to do so with exception below:
System.NotSupportedException: 'Unsupported expression: x => x.Where(It.IsAny<Expression<Func<Consent, bool>>>()) Extension methods (here: Queryable.Where) may not be used in setup / verification expressions.'
Been stuck on this for over 4 days with zero steps forward. I can't do anything with a code. Only able to write tests for the project. I know that expressions is complicated to mock but I'm looking forward to solve it.