mock HttpContext.Current.Server.MapPath using Moq?

前端 未结 4 1927
一生所求
一生所求 2020-12-31 08:26

im unit testing my home controller. This test worked fine until I added a new feature which saves images.

The method that’s causing the issue is this below.

4条回答
  •  抹茶落季
    2020-12-31 09:19

    I agree with the Darin´s answer but if you really need to moq the Server.MapPath function you could do something like this

    //...
    var serverMock = new Mock(MockBehavior.Loose);
    serverMock.Setup(i => i.MapPath(It.IsAny()))
       .Returns((String a) => a.Replace("~/", @"C:\testserverdir\").Replace("/",@"\"));
    //...
    

    Performing this, the mock will simply replace the ~/ with the c:\testserverdir\ function

    Hope it helps!

提交回复
热议问题