0

I have handler interface and its interface implemented class

public class Microsoft365Handler :  IMicrosoft365Handler
{ 
 
   protected internal void DeleteDocument()
  {
    
  }
}

And handler gets called from another class like below

internal class Microsoft365Agent : AgentBase, IMicrosoft365Agent
{
  public void DeleteDocument(Microsoft365Model microsoft365Model)
     {
        var client = Create<Microsoft365Handler>();
        client.DeleteDocument();           
     }
}

below is my test case

[TestMethod]
public void Microsoft365Agent_DeleteDocument_Success()
{
    // Create a fake authentication context.
    Microsoft365Model microsoft365Model = new Microsoft365Model
    {
        Id = 1,
        AccessToken = "ABCSSDSDSD",
        RefreshToken = "JGJIBJKBKJB",
        ClientId = "HJBJKBHBKHBK",
        ClientSecret = "HHUSDHDBSD",
        RemoteFolder = "NORTHGATE/Test",
        TenantId = "KJJKBJBKB",
        UserId = "imasys",
        UserEmailAddress = "",
        DocGuid = CommonHelper.OFFICE365UPLOADEDFILE,
        DocumentFilePath = "",
        FileName = "Test.Docx",
        LastTokenRefreshDate = DateTime.Now,
        Office365EmailAddress = "",
        UploadedDocumentUrl = ""
    };

    try
    {
        using (var agent = AgentFactory.Create<IMicrosoft365Agent>(securityInfo))
        {
            agent.DeleteDocument(microsoft365Model);
        }
        return;
    }
    catch (Exception ex)
    {
        Assert.Fail(ex.Message);
    }
}

its called delete method from concrete class but when for test cases I not able to create mock of handler method as its mark as protected internal.

We have unity to implement unit test cases so that we can have fake classes to handle unit test case but in this particular method I want redirect hander method to another fake class but I cant change its access modifiers (cant upgrade to c#8.0). I there any way to re-design or write test case?

  • >We have unity to implement unit test cases so that we can have fake classes to handle unit test could you clarify that sentence? "Unity" is commonly known as a game engine not any unit-test related thing. In any case are you using any mocking framework like moq? – T.Schwarz Jul 07 '21 at 06:44
  • I think you need a Test Server that return non standard response. So you need to be able to change the URI of the request to a different server that returns test responses. – jdweng Jul 07 '21 at 06:44
  • @T.Schwarz yes we are using Microsoft unity for DI and that help us to create mock for test environment – Vivek Chavan Jul 07 '21 at 07:08
  • @jdweng actually i need to create kind of mock on runtime for handler method as its not defined in interface – Vivek Chavan Jul 07 '21 at 07:10
  • Even after rereading it several times it's completely unclear to me what you're after. Can you give an example of a test as you'd like to see it (complete with setup) which fails to compile? – Jeroen Mostert Jul 07 '21 at 08:45
  • @JeroenMostert I updated my question, point is how to create mock of method which is not defined in interface. – Vivek Chavan Jul 07 '21 at 09:40
  • you can use moq: https://stackoverflow.com/questions/37473996/with-moq-how-can-i-mock-protected-methods-with-out-parameter Not sure if it works with internal, look it up and play around – Ric Jul 07 '21 at 09:44

0 Answers0