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
I have spent better part of today trying to dive back into mocking. I have some rather simple code that I need to test that has proven to be a lot harder to test then I would have thought.

Much of that is because of a series of Helper methods that get called, all working w/in the HttpContext object.

I have managed to get nearly complete except for when I try to "mock" MapPath. Here is what I have so far:

this._mockHttpContext.ExpectGetAlways("Current", this._httpContext);
this._mockHttpContext.ExpectGetAlways("Request", this._httpRequest);
this._mockHttpRequest.ExpectGetAlways("ApplicationPath", "/pixelmedia.cmt.web/");

string dirRoot = PixelMEDIA.CMT.Core.Helpers.ConfigHelper.GetAppSetting("DIR_ROOT");
this._mockHttpRequest.ExpectGetAlways("MapPath", dirRoot);


It fails on the last line with the following error:

TestCase 'PixelMEDIA.CMT.Tests.Unit.Core.Controls.Server.XsltServerControlTestFixture.TestXsltOverride'
failed: TypeMock.TypeMockException :
*** No method get_MapPath in type System.Web.HttpRequest
at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Type[] A_4)
at TypeMock.Mock.AlwaysReturn(String method, Object ret, Type[] genericTypes)
at TypeMock.Mock.ExpectGetAlways(String property, Object value)
C:clientWorkPixelMEDIACMTsrcTestsUnitCoreControlsServerXsltServerControlTestFixture.cs(42,0): at PixelMEDIA.CMT.Tests.Unit.Core.Controls.Server.XsltServerControlTestFixture.TestXsltOverride()


I'm at a loss now on how to proceed. Any suggestions are greatly appreciated.

Thanks.
asked by kyleheon (3.1k points)

3 Answers

0 votes
Hi,

MapPath is not a property of HttpRequest its a regular method.
in order to mock it use:

this._mockHttpRequest.AlwaysReturn("MapPath", dirRoot);
answered by lior (13.2k points)
0 votes
Many thanks for that tip. My tests are working as expected now. Now, to further my understanding of TypeMock, how would this test look if I was to used Natural Mocks? Can I even do that w/this?
answered by kyleheon (3.1k points)
0 votes
Hi,

Using Natural Mock you test would most likely look something like:
string dirRoot = PixelMEDIA.CMT.Core.Helpers.ConfigHelper.GetAppSetting("DIR_ROOT");

using (RecordExpectations rec = new RecordExpectations())
{
    HttpContext dummyContext = this.MyHttpContext.Current;
    rec.Return(this._httpContext).RepeatAlways();
    HttpRequest dummtRequest = this.MyHttpContext.Request;
    rec.Return(this._httpRequest).RepeatAlways();
    string dummyPath = this.MyHttpRequest.ApplicationPath;
    rec.Return("/pixelmedia.cmt.web/").RepeatAlways();
    dummyPath = this.MyHttpRequest.MapPath();
    rec.Return(dirRoot).RepeatAlways();
}

...do actual test...


You might also like to mock the creation of the HttpContext and HttpRequest which can easily be done very much alike.

:!: since everything in a recording block get mocks the creation of dirRoot was taken outside the block.

Hope this helps
answered by lior (13.2k points)
...