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 this method that I'm testing:

private ServiceFlowRequest BuildRequest(HttpContext httpContext, Page page)
{
    ServiceFlowRequest serviceFlowRequest = null;

    serviceFlowRequest = new ServiceFlowRequest();
    serviceFlowRequest.HttpContext = httpContext;
    serviceFlowRequest.Page = page;
    serviceFlowRequest.ServiceFlowId = serviceFlowRequest.ServiceFlowVersion = null;
    serviceFlowRequest.ServiceId = serviceFlowRequest.StepId = null;

    string token = httpContext.Request[SERVICE_FLOW_REQUEST_PARAMETER_NAME];
    if (!string.IsNullOrEmpty(token))
    {
        Guid instanceId;
        Guid version;
        if (this.m_instanceCorrelationTokenProvider.TryDecrypt(token, out instanceId, out version))
        {
            serviceFlowRequest.ServiceFlowId = instanceId;
            serviceFlowRequest.ServiceFlowVersion = version;
        }
        return serviceFlowRequest;
    }

    int serviceId;
    string serviceIdString = httpContext.Request[SERVICE_REQUEST_PARAMETER_NAME];
    if (!string.IsNullOrEmpty(serviceIdString) && int.TryParse(serviceIdString, out serviceId))
    {
        string stepIdString = httpContext.Request[STEP_REQUEST_PARAMETER_NAME];
        if (!string.IsNullOrEmpty(stepIdString))
        {
            int stepId;
            if (!int.TryParse(stepIdString, out stepId))
            {
                return serviceFlowRequest;
            }
            serviceFlowRequest.StepId = stepId;
        }
        serviceFlowRequest.ServiceId = serviceId;
    }
    return serviceFlowRequest;
}


I have tests to assure 100% code coverage of the tested method.

This test:

public void BuildRequestWithoutServiceIdTest()
{
    global::Esi.FlexView.PL.Web.ServiceFlow.IServiceFlowInstanceCorrelationTokenProvider instanceCorrelationTokenProvider = RecorderManager.CreateMockedObject<global::Esi.FlexView.PL.Web.ServiceFlow.IServiceFlowInstanceCorrelationTokenProvider>(Constructor.Mocked, StrictFlags.AllMethods);

    global::Esi.FlexView.PL.Web.ServiceFlow.ServiceFlowProvider target = RecorderManager.CreateMockedObject<global::Esi.FlexView.PL.Web.ServiceFlow.ServiceFlowProvider>(Constructor.Mocked, StrictFlags.AllMethods);

    global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor accessor = new global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor(target);
    accessor.m_instanceCorrelationTokenProvider = instanceCorrelationTokenProvider;

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

    global::System.Web.UI.Page page = RecorderManager.CreateMockedObject<global::System.Web.UI.Page>(Constructor.Mocked, StrictFlags.AllMethods);

    string serviceFlowKey = global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor.SERVICE_FLOW_REQUEST_PARAMETER_NAME;
    string serviceIdKey = global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor.SERVICE_REQUEST_PARAMETER_NAME;

    string token = null;
    global::System.Guid instanceId = global::System.Guid.Empty;
    global::System.Guid version = global::System.Guid.Empty;

    int serviceId = 10;
    string serviceIdString = "10";

    global::System.Web.HttpContext expectedHttpContext = httpContext;
    global::System.Web.UI.Page expectedPage = page;
    global::System.Guid? expectedInstanceId = null;
    global::System.Guid? expectedVersion = null;
    int? expectedServiceId = null;
    int? expectedStepId = null;

    global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProvider_ServiceFlowRequestAccessor actual;

    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        recorder.DefaultBehavior.CheckArguments();

        recorder.ExpectAndCallOriginal(accessor.BuildRequest(httpContext, page));

        recorder.ExpectAndReturn(httpContext.Request[serviceFlowKey], token);

        recorder.FailWhenCalled(instanceCorrelationTokenProvider.TryDecrypt(token, out instanceId, out version));

        recorder.ExpectAndReturn(httpContext.Request[serviceIdKey], serviceIdString);

        recorder.FailWhenCalled(int.TryParse(serviceIdString, out serviceId));
    }

    actual = accessor.BuildRequest(httpContext, page);

    // Asserts that do not call on any mocked instances.
}


This method allways fails with
TypeMock Verification: Unexpected Call to System.Web.HttpRequest.get_Item().
on the call to
httpContext.Request[SERVICE_REQUEST_PARAMETER_NAME]
in the tested method.

It get's even more strange because these methods don't fail:

public void BuildRequestWithServiceIdNotParsedTest()
{
    global::Esi.FlexView.PL.Web.ServiceFlow.IServiceFlowInstanceCorrelationTokenProvider instanceCorrelationTokenProvider = RecorderManager.CreateMockedObject<global::Esi.FlexView.PL.Web.ServiceFlow.IServiceFlowInstanceCorrelationTokenProvider>(Constructor.Mocked, StrictFlags.AllMethods);

    global::Esi.FlexView.PL.Web.ServiceFlow.ServiceFlowProvider target = RecorderManager.CreateMockedObject<global::Esi.FlexView.PL.Web.ServiceFlow.ServiceFlowProvider>(Constructor.Mocked, StrictFlags.AllMethods);

    global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor accessor = new global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor(target);
    accessor.m_instanceCorrelationTokenProvider = instanceCorrelationTokenProvider;

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

    global::System.Web.UI.Page page = RecorderManager.CreateMockedObject<global::System.Web.UI.Page>(Constructor.Mocked, StrictFlags.AllMethods);

    string serviceFlowKey = global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor.SERVICE_FLOW_REQUEST_PARAMETER_NAME;
    string serviceIdKey = global::Web.Tests.Esi_FlexView_PL_Web_ServiceFlow_ServiceFlowProviderAccessor.SERVICE_REQUEST_PARAMETER_NAME;

    string token = null;
    global::System.Guid instanceId = global::System.Guid.Empty;
    global::System.Guid version = global::System.Guid.Empty;

&
asked by paulo.morgado (11k points)

3 Answers

0 votes
Hi Paulo.
Can you please send me the tracer output?
I'll send you my address offline.
answered by ohad (35.4k points)
0 votes
Paulo hi,

The problem is caused by this statement:
recorder.FailWhenCalled(int.TryParse(serviceIdString, out serviceId));

In General TypeMock does not handle calls from mscorlib and specifically can't fail when they are called. (In this case due to the special way we are handling Natural Mocking it caused a confusion making TypeMock fail the last mocked method i.e. HttpRequest[...])

However we should, as we usually do, throw an exception when this is attempted so we will need to fix this, thank you for reporting it.

:!: There is a known issue with the FailWhenCalled API that we are currently working on that causes it to fail during verification (a work around is to use RepeatAlways on these calls)
answered by lior (13.2k points)
0 votes
I think the main problem here was trying to record an expectation on int.TryParse because int (System.Int32) is on mscorlib.dll.

I changed that expectation for another one and now it works fine.
answered by paulo.morgado (11k points)
...