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
When testing:

httpContext.Response.Redirect(httpContext.Response.ApplyAppPathModifier(httpContext.Request.RawUrl), true);


I was expecting that this would do it:

global::System.Web.HttpContext httpContext = RecorderManager.CreateMockedObject<global::System.Web.HttpContext>(Constructor.Mocked, StrictFlags.AllMethods);
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
  httpContext.Response.Redirect(httpContext.Response.ApplyAppPathModifier(httpContext.Request.RawUrl), true);
}


But it fails with:
TypeMock.TypeMockException:
*** Mocked return value of HttpContext.get_Response() is unknown, use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly..


Not what I expected, but it can be solved by changing the test to:

global::System.Web.HttpContext httpContext = RecorderManager.CreateMockedObject<global::System.Web.HttpContext>(Constructor.Mocked, StrictFlags.AllMethods);
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
  recorder.ExpectAndReturn(global::System.Web.Security.FormsAuthentication.CookiesSupported, false);
  recorder.ExpectAndReturn(httpContext.Request.RawUrl, "httpContext.Request.RawUrl");
  recorder.ExpectAndReturn(httpContext.Response.ApplyAppPathModifier(null), "httpContext.Response.ApplyAppPathModifier()");
  httpContext.Response.Redirect(null, true);
}


Now the tested code is failing with a NullReferenceException.

I had to change the tested code to:

string redirectUrl = httpContext.Response.ApplyAppPathModifier(httpContext.Request.RawUrl);
httpContext.Response.Redirect(redirectUrl, true);


What am I doing wrog?
asked by paulo.morgado (11k points)

6 Answers

0 votes
Paulo hi,

Inside a recording block ALL calls are recorded and mocked, including (in the first code example) the calls to
httpContext.Response and httpContext.Reuest. That is the reason you got the exception on your first try.
When you rewrtoe the using block you specified what should be returned by this calls.

The NullReferenceException execption you got afterwards is not something that should happen. We will investigate it and let you know what is happening there.

:idea: Using TypeMock Tracer will show a lot of information on which calls are getting mocked.
answered by lior (13.2k points)
0 votes
I was expecting ALL calls to be recorded and mocked, like it happens in:

recorder.ExpectAndReturn(httpApplication.Session["someKey"], null);
answered by paulo.morgado (11k points)
0 votes
I just came across another case:

global::System.Web.HttpApplication httpApplication = RecorderManager.CreateMockedObject<global::System.Web.HttpApplication>(Constructor.Mocked, StrictFlags.AllMethods);

global::System.Web.Security.FormsAuthenticationModule formsAuthenticationModule = RecorderManager.CreateMockedObject<global::System.Web.Security.FormsAuthenticationModule>(Constructor.Mocked, StrictFlags.AllMethods);

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
    recorder.ExpectAndReturn(httpApplication.Modules["FormsAuthentication"], formsAuthenticationModule); // This is line X
}


This is the stack trace of the exception:

System.NullReferenceException: Object reference not set to an instance of an object.
at TypeMock.RecorderManager.a(Object A_0, String A_1, String A_2, StackTrace A_3)
at TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Object[] A_3, MethodBase A_4, Object A_5, StackTrace A_6)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1)
at System.Web.AspNetHostingPermissionAttribute..ctor(SecurityAction action)
at System.Web.HttpApplication.get_Modules()
at MyTest() in ...MyFixture.cs:line X
answered by paulo.morgado (11k points)
0 votes
This last case seems to happen every time an System.Web.AspNetHostingPermissionAttribute is applied to the method.

I tried asserting and demanding the permission but still fails.
answered by paulo.morgado (11k points)
0 votes
Hi,
You have reported a few problems here.
I can verify that we have managed to reproduce them.
Thank you for reporting them with examples.

We are currently fixing them and will send you a patch as soon as we can.
answered by scott (32k points)
0 votes
Thanks Scott.
answered by paulo.morgado (11k points)
...