Home > front end >  How to instantiate a class with base in the constructor?
How to instantiate a class with base in the constructor?

Time:12-16

I have the below class, I am trying to write unit test to test the UnhandledException method:

public class Workflow :WorkFlowManager
{
    public Workflow() : base(Path.Combine(ConfigurationManager.AppSettings["ConfigurationFilesLocation"], confFileName))
    {
        this.RegistrationOutput = new Output();
    }

    public override async Task UnhandException(ModuleInterface module, Exception ex)
    {
        
    }
}

I'm using xUnit and moq frameworks, and I'm confused about how to implement the constructor parameters.

Furthermore, I tried this Test:

[Fact]
public void UnhandledExceptionTest()
{
    var cls = new Workflow();
    var mock = new Mock<ModuleInterface>();
    var ser = new Mock<AServices>();
    var InteractionService = new Mock<InteractionService>();
    var iSharedLogService = new Mock<ISharedLogService>();
    Exception ex = new Exception();
    cls.UnhandException(mock.Object, ex);
}

But I'm having a System.ArgumentNullException because Path.Combine(String path1, String path2) in the constructor of Workflow can't be null.

CodePudding user response:

I've made a quick proof-of-concept to show that Path.Combine doesn't accept null arguments, so that means that either ConfigurationManager.AppSettings["ConfigurationFilesLocation"] or confFileName is null. You will have to fix those values; unfortunately if you can't modify the class there's nothing else you can do.

Also, please tell your colleagues that this is not how you write testable code! Calling static methods in a base constructor makes it almost impossible to test nicely.

  • Related