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;
&