vendredi 14 juin 2019

How to do stubbing of Private Method

I have WebAPI controller Method(HTTPPost) , which further call a private method which is used to get the some data from WCF service.

While writing unit test for HTTPPost method, private method getting crash due to service is not reachable. Is their any way , so that I can define new definition to the private method for my test case.

I had reflection to redefine the private method and issue is that I can;t change any thing in the code except unit test project.

public class SController : ControllerBase
{    
[HttpPost()]
public IActionResult X()
{
 Y();
}

private bool Y()
{
 Z z = new Z();  // WCF Partial class object 
 bool b = z.GetInfoAsync(a,b,c) ; // WCF method calling
}
}

Is Their any way , I can X method of my controller but when X trying to call Y , it should return define value in the test case instead of service call.

Aucun commentaire:

Enregistrer un commentaire