jeudi 7 novembre 2019

Issue in dependency injection while implementing Unit testing in MVC6

-> My Unit testing Code is

[TestClass]
public class ProjectControllerTest
{
    Mock<IProjectService> addressRepositoryMock = new Mock<IProjectService>();
    ProjectController controller;

    [TestInitialize]
    public void TestInit()
    {
        controller = new ProjectController(addressRepositoryMock.Object);
    }
    [TestMethod]
    public void TestMethod1()
    {
        var response = controller.GetOverrides(1) as HttpResponseMessage;
    }
}

-> my controller code is

[Route("GetOverrides")]
    [HttpGet]
    public HttpResponseMessage GetOverrides(int Project_Id)
    {
        List<OverridesDto> overrides = new List<OverridesDto>();
        try
        {
            overrides = projectService.GetOverrides(Project_Id);
        }
        catch (Exception ex)
        {
            Log.Logger.Error(ex, "Exception:{Ex}", null);
            ThrowHttpExceptionOnError(ex.ToString());
        }
        return Request.CreateResponse(HttpStatusCode.OK, overrides);
    }

-> i can able hit method getoverides in controller from unit testing project in debug mode but the problem is i can not hit BAL method projectService.GetOverrides(Project_Id); which returning null

please help me out with the above issue.

Aucun commentaire:

Enregistrer un commentaire