1

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());
tanmayghosh2507
  • 773
  • 3
  • 12
  • 31
  • The question in its current state is incomplete and therefore unclear. – Nkosi Feb 15 '20 at 00:48
  • How are you creating mock of `IDependencyResolver`? – Chetan Feb 15 '20 at 01:00
  • @ChetanRanpariya Please check the updated question. – tanmayghosh2507 Feb 15 '20 at 01:06
  • @Nkosi Added some details, please let me know if you need more details. In a nutshell though, I just want to be able to write unit tests for my piece of code above. – tanmayghosh2507 Feb 15 '20 at 01:07
  • What you are doing looks ok. What issue you are facing? – Chetan Feb 15 '20 at 01:11
  • Okay so, when I am setting up the sampleLogger and requestSender like dependencyResolverMock.Setup(x => x.Resolve>().Configure()).Returns(// a dummy requestSender type); and further mock any operations on the requestSender object, it is not considering the mocked object, and giving me Null reference exception, because the operations seems to be happening on the actual object. – tanmayghosh2507 Feb 15 '20 at 01:16
  • I See no relation between the `TestClass` and `FunctionToTest`. What are you **actually** trying to do? This might be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Nkosi Feb 15 '20 at 02:03
  • 1
    You just have to mock `Resolve` method and return appropriate mocked instances... `Setup(d => d.Resolve()).Returns(Mock.Of)` and so on... – Johnny Feb 15 '20 at 12:50
  • Thanks Johnny! It worked. – tanmayghosh2507 Feb 18 '20 at 20:16

1 Answers1

0

Having this in the test constructor worked fine for me:

public ReportingTest()
{
    dependencyResolverMock = new Mock<IDependencyResolver>();

    dependencyResolverMock.Setup(x => x.Resolve<ISampleLogger<SampleReporting>>()).Returns(Mock.Of<ISampleLogger<SampleReporting>>());
    dependencyResolverMock.Setup(x => x.Resolve<IRequestSender()).Returns(Mock.Of<IRequestSender>());
}
tanmayghosh2507
  • 773
  • 3
  • 12
  • 31