所以我正在创建一个Mocked HttpContextBase:
Public Function CreateFake(iP As String) As HttpContextBase
Dim uri As New Uri("http://google.com/")
Dim context As New Mock(Of HttpContextBase)
Dim request As New Mock(Of HttpRequestBase)
request.Setup(Function(req) req.UserHostAddress).Returns(iP)
Return context.Object
End Function
目前我只需要IP地址,所以这是我设置的唯一内容。
现在在我的单元测试中,我需要设置当前的http上下文,因为它用于检查以后函数中的用户IP地址。
单元测试:
<TestMethod()> Public Sub SetSessionVariablesBasedOnUserLocation_IPIs192168180_LocalNetworkTrueAndIPAddrMatchesMyIP()
Dim x = FakeHttpContext.CreateFake("192.168.1.10")
'Errors here as x is a HttpContextBase
HttpContext.Current = x
_sessionManager.SetSessionVariablesBasedOnUserLocation()
End Sub
然后在SetSessionVariablesBasedOnUserLocation中我有这条线用于获取用户IP:
Dim remoteAddr = HttpContext.Current.Request.UserHostAddress
但是,我无法将x传递给HttpContext。
我该怎么做?