chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi there.

I'm trying to mock the following in my web control:

Dim urlRequestedByUser As String = Page.Request.QueryString(m_queryStringName)


Basically, when the code calls Page.Request.QueryString(m_queryStringName) I would like to return a mock string value.

I'm difficulty in mocking this code, can anyone suggest something that can help?

Many thanks.
Jas.
asked by MrClyfar (5.2k points)

2 Answers

0 votes
Hi, look at the thread here: https://www.typemock.com/community/viewt ... quest#2744

tillig gives examples how to mock HTTPWebRequest object (the one you are trying to mock) both in natural and reflective mocks.

Once you have the Request object mocked, you can set a "ExpectAndReturn" on the QueryString method.

Let me know if you need further help.
answered by gilz (14.5k points)
0 votes
Hi there.

Ok, after some trial and error I got the following to work:

Dim mockPage As System.Web.UI.Page = RecorderManager.CreateMockedObject(GetType(System.Web.UI.Page), Constructor.NotMocked)
        Dim mockHTTPRequest As System.Web.HttpRequest = RecorderManager.CreateMockedObject(GetType(System.Web.HttpRequest), Constructor.Mocked)
        Dim mockQueryString As System.Collections.Specialized.NameValueCollection = RecorderManager.CreateMockedObject(GetType(System.Collections.Specialized.NameValueCollection), Constructor.NotMocked)
        
        ' Add a default value to the querystring.
        mockQueryString.Add("requestedUrl", "http://www.google.com")

        Using recorder As RecordExpectations = RecorderManager.StartRecording
            Dim dummy As Object = mockPage.Request
            recorder.Return(mockHTTPRequest)

            Dim dummy2 As Object = mockHTTPRequest.QueryString
            recorder.Return(mockQueryString)
        End Using


So in my web control, the code:

Page.Request.QueryString


is now mocked nicely. Cool.

Cheers for the help.
Jas.
answered by MrClyfar (5.2k points)
...