views:

325

answers:

2

I'm trying to implement the GetHttpContext function from HtmlHelperTest.cs in VB.NET using Rhino Mocks, but I'm getting "Type 'HttpContextBase' is not defined." The compiler suggests changing it to HttpContext, but when I do that I get a run time error that a sealed class cannot be mocked.

My test project references System.Web and also imports that namespace. Is there something else I need to do in order to get the HttpContextBase type to be visible to the code I'm witting?

Here's the code, but I don't know how relevant it is to the problem.

Imports NUnit.Framework
Imports Rhino.Mocks
Imports System.Web.Routing

<TestFixture()> _
Public Class XhtmlHelperTest

    Public Const AppPathModifier = "/$(SESSION)"

    'Various test methods here...'

    Private Shared Function GetHttpContext(ByVal appPath As String, _
                                           ByVal requestPath As String, _
                                           ByVal httpMethod As String, _
                                           Optional ByVal protocol As String = "http", _
                                           Optional ByVal port As Integer = -1 _
                                           ) As HttpContextBase
        Dim mockHttpContext = MockRepository.GenerateMock(Of HttpContextBase)()

        If Not String.IsNullOrEmpty(appPath) Then
            mockHttpContext.Expect(Function(hc) hc.Request.ApplicationPath).Return(appPath)
        End If

        If Not String.IsNullOrEmpty(requestPath) Then
            mockHttpContext.Expect(Function(hc) hc.Request.AppRelativeCurrentExecutionFilePath).Return(requestPath)
        End If

        Dim uri As Uri

        If port >= 0 Then
            uri = New Uri(protocol + "://localhost" + ":" + port)
        Else
            uri = New Uri(protocol + "://localhost")
        End If

        mockHttpContext.Expect(Function(hc) hc.Request.Url).Return(uri)

        mockHttpContext.Expect(Function(hc) hc.Request.PathInfo).Return("")

        If Not String.IsNullOrEmpty(httpMethod) Then
            mockHttpContext.Expect(Function(hc) hc.Request.HttpMethod).Return(httpMethod)
        End If

        mockHttpContext.Expect(Function(hc) hc.Response.ApplyAppPathModifier(Arg(Of String).Is.Anything)).WhenCalled(Function(invocation) AppPathModifier + invocation.Arguments(0))

        Return mockHttpContext
    End Function

End Class
+5  A: 

You need to add a reference to System.Web.Abstractions.

tvanfosson
Thanks, that did it. When I looked at the code from MvcDev in Visual Studio it said that the class was in System.Web.
Dennis Palmer
Oh, I see! It is in the System.Web namespace, but not in the System.Web assembly. That's confusing!
Dennis Palmer
+1  A: 

Have you included the System.Web.Abstractions assembly and import System.Web?

Rex M