Call a service method using mock in unit test case

时间:2017-06-15 10:22:34

标签: asp.net-mvc unit-testing c#-4.0 mocking

I need to Call a service method using mock in unit test case which pass through from controller. In internet most of the solutions are like, directly return service value. I need to call the service method from controller after called the controller method from unit test case. It should get the return values from DB.

//My Unit test case
var mapBlockServiceMock = new Mock(IMapBlockService)( );

//Here I am doing setup for services(but it will directly return a value)
mapBlockServiceMock.Setup(service => service.GetMapBlock(1)) .Returns(GetMapBlockList());

var controller = new MapDashboardController(mapBlockServiceMock.Object);

int mapId = 1;   
 var values = controller.GetHomePage(mapId);

//My Controller
private readonly IMapBlockService mapblockservice;

public MapDashboardController(IMapBlockService mapblockservice)  
    {
       this.mapblockservice = mapblockservice  
    }

public HomePageViewModel GetHomePage ( int mapId )  
        {  
List<MapBlock> mapBlockList= new List<MapBlock>();  
mapBlockList = mapblockservice.GetMapBlock(mapId)  
}

Now using mock services, it should go to the mapblockservice.GetMapBlock.

Thanks in Advance.

0 个答案:

没有答案