I'm trying to write Unit Tests in C# .Net Core 2.1 Function App with xUnit framework for something like this, where the class variables are initialized through Autofac dependency injection.
public class TestClass
{
private readonly ISampleLogger<SampleReporting> sampleLogger;
private readonly IRequestSender requestSender;
public SampleReporting(IDependencyResolver dependencyResolver)
{
dependencyResolver?.Resolve<ISampleLoggingStartup>().Configure();
this.sampleLogger = dependencyResolver.Resolve<ISampleLogger<SampleReporting>>();
this.requestSender = dependencyResolver.Resolve<IRequestSender>();
}
...
public void FunctionToTest(string s)
{
...
I'm trying to mock the dependencyresolver object but couldn't figure out how to mock the variables sampleLogger and requestSender as they are getting set internally in the constructor. Any leads will be appreciated.
I tried to mock IDependencyResolver like this, but could not get it to work:
private readonly Mock<IDependencyResolver> dependencyResolverMock;
[Fact]
public void Test1()
{
dependencyResolverMock.Setup(x => x.Resolve<It.IsAny<ISampleLoggingStartup>>().Configure());