mardi 22 décembre 2015

How to assign a mocked object to a object in controller?

I have a controller which contains a business class that internally has dependencies to a datahandler. For testing that business class I need to mock the datahandler. After setup, I am assigning the business class' datahandler with the mocked datahandler. But while debugging, the business class' datahandler is showing null , I know that I should use the constructor to inject the mocked object.But is it possible to do it without using any constructor injection ?Can any body help me with this?

my business class:

public class FooBusiness
{
  public static BarDataHandler _barDatahandler = new BarDataHandler();
  ...
}

Test class:

public class FooBusinessTest
{
  ...
  _mockedBarDataHandler = new Mock<IBarDataHandler>(){CallBase:true};

   public FooTestMeth()
   {
    //Arrange
    _mockedBarDataHandler.Setup(x=>x.Search(It.IsAny<int>).Returns(1);
    ...
    FooBusiness _fooBusiness = new FooBusiness();

    FooBusiness._barDatahandler = _mockedBarDataHandler.Object;

    //Act
    ...
   }
}

Aucun commentaire:

Enregistrer un commentaire