MVC:如何在使用HttpContextWrapper转换后进行模拟

时间:2013-11-12 19:18:08

标签: asp.net-mvc unit-testing rhino-mocks

Public Class PhotoUploadController

   Public ReadOnly Property IsMobileDevice As Boolean
       Get
          Return ControllerContext.HttpContext.GetOverriddenBrowser.IsMobileDevice
       End Get
   End Property

   Function SavePhoto(model As PhotoUploadModel) As ActionResult
       If Not String.IsNullOrWhiteSpace(Request.Files(0).FileName) And IsMobileDevice Then
    Return View("Index", model)
   End If       
   End Function

End Class   

<TestMethod()>
 Public Sub SavePhoto_Test()

   Dim permissions As New List(Of String)
   permissions.Add(Constants.VIEW_ACCOUNTS)
   Dim mUser As New MockUser(ListOfPermissions:=permissions)
   Dim controller As PhotoUploadController = New PhotoUploadController(mUser, New PhotoRepository)
   System.Web.HttpContext.Current = New HttpContext(New HttpRequest("test", "http://www.yahoo.com/accounts", ""), New HttpResponse(New System.IO.StringWriter()))

   Dim browserMock = MockRepository.GenerateStub(Of HttpBrowserCapabilities)()
   browserMock.Expect(Function(b) b.IsMobileDevice).Return(True)
   System.Web.HttpContext.Current.Request.Browser = browserMock

   Dim currentContext As HttpContextBase = MockRepository.GenerateStub(Of HttpContextWrapper)(System.Web.HttpContext.Current)
   currentContext.Expect(Function(fn) fn.Request.Files(0).FileName).Return("test.txt")
   'If I comment out the two lines above and uncomment the below line the IsMobile is set in the SavePhoto actionresult otherwise it is null.
   'Dim currentContext As HttpContextBase = New HttpContextWrapper(System.Web.HttpContext.Current)
   controller.ControllerContext = New ControllerContext(currentContext, New System.Web.Routing.RouteData(), controller)

   Dim model As New PhotoUploadModel(mUser)

   Dim result As ActionResult = controller.SavePhoto(model)

   Assert.IsNotNull(result)
   Assert.IsInstanceOfType(result, GetType(ViewResult))

End Sub

1 个答案:

答案 0 :(得分:1)

以下答案应引导您找到正确的方向。 注意如果我的语法不正确,我不是一个VB.NET开发人员。我实际上使用了一个工具将C#转换为VB:)

首先,您的单元测试存在一些问题

一个。您测试的方法名称写得不好,但它并不反映您的确切意图是什么。

的存在/设定期望并不容易
  

ControllerContext.HttpContext.GetOverriddenBrowser.IsMobileDevice

GetOverriddenBrowser是一种静态扩展方法。所以它不可模糊。

℃。你的断言正在验证两件事。首先是结果是否为null,其次是结果类型是ViewResult。我会亲自测试后者,其结果类型。如果结果不是,测试会以任何方式抛出异常。通过限制为一个断言,您可以更轻松地编写测试方法名称。

为了避免剔除的痛苦.IsMobileDevice我只是在Controller中简单地引入一个属性/或多或少的Func / delegate属性。我会在构造函数中设置IsMobileDevice,如下所示。这样我就可以在我的单元测试中简单地存储从IsMobileDeviceFunc返回的值。

Imports System.Web.WebPages

Public Interface IUser
End Interface

Public Interface IPhotoRepository
End Interface

Public Class PhotoUploadModel
End Class

Public Class PhotoUploadController
    Inherits Controller

    Private m_IsMobileDeviceFunc As Func(Of Boolean)

    Public Sub New(user As IUser, repository As IPhotoRepository)
        IsMobileDeviceFunc = Function() 
        ControllerContext.HttpContext.GetOverriddenBrowser().IsMobileDevice
    End Sub

    Public Property IsMobileDeviceFunc() As Func(Of Boolean)
        Get
            Return m_IsMobileDeviceFunc
        End Get
        Set(value As Func(Of Boolean))
           m_IsMobileDeviceFunc = Value
        End Set
    End Property


    Public Function SavePhoto(model As PhotoUploadModel) As ActionResult
       Dim isMobD = IsMobileDeviceFunc

       If Not String.IsNullOrWhiteSpace(Request.Files(0).FileName) And isMobD() Then
           Return View("Index", model)
       End If

       Return New EmptyResult()
    End Function

End Class

单元测试

 <TestMethod()> Public Sub SavePhoto_ActionExecute_ActionResultIsTypeOfViewResult()

    Dim sut = New PhotoUploadController(MockRepository.GenerateStub(Of IUser)(), MockRepository.GenerateStub(Of IPhotoRepository)())
    sut.IsMobileDeviceFunc = Function() True
    Dim currentContext = MockRepository.GenerateStub(Of HttpContextBase)()
    Dim requestStub = MockRepository.GenerateStub(Of HttpRequestBase)()
    requestStub.Expect(Function(x) x.Files(0).FileName).[Return]("foo")
    currentContext.Expect(Function(x) x.Request).[Return](requestStub)
    sut.ControllerContext = New ControllerContext(currentContext, New RouteData(), sut)

    Dim result As ActionResult = sut.SavePhoto(New PhotoUploadModel())

    Assert.IsInstanceOfType(result, GetType(ViewResult))
 End Sub

enter image description here

希望这会指向正确的方向。