You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Facing this issue when mocking GetRequiredService
Actual function
private readonly IServiceScopeFactory _provider;
using var scope = _provider.CreateScope();
var queueRepo = scope.ServiceProvider.GetRequiredService<IQueuedMailRepository>();
await queueRepo.Remove(queueEmailDetail.Id);
How I'm trying to mock the injection while testing.
var mockServiceFactory = new Mock<IServiceScopeFactory>();
var mockServiceProvider = new Mock<IServiceProvider>();
var mockServiceScope = new Mock<IServiceScope>();
var mockRepo = new Mock<IQueuedMailRepository>();
mockServiceFactory.Setup(x => x.CreateScope()).Returns(mockServiceScope.Object);
mockServiceScope.Setup(x => x.ServiceProvider).Returns(mockServiceProvider.Object);
mockServiceProvider.Setup(x => x.GetRequiredService<IQueuedMailRepository>()).Returns(mockRepo.Object);
// OR
mockServiceProvider.Setup(x => x.GetRequiredService(typeof(IQueuedMailRepository))).Returns(mockRepo.Object);
Getting following error at last line of mock at run time
Message:
System.NotSupportedException : Unsupported expression: x => x.GetRequiredService(Mailer.DataAccess.Contracts.IQueuedMailRepository)
Extension methods (here: ServiceProviderServiceExtensions.GetRequiredService) may not be used in setup / verification expressions.
The text was updated successfully, but these errors were encountered:
Yep, Moq doesn't support setting up extension methods, and there is nothing we can do to change that. You can only set up & verify overridable methods.
Facing this issue when mocking
GetRequiredService
Actual function
How I'm trying to mock the injection while testing.
Getting following error at last line of mock at run time
The text was updated successfully, but these errors were encountered: