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 I record chained expectations like this:

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
  recorder.ExpectAndReturn(httpContext.Request.AppRelativeCurrentExecutionFilePath, requestedPath).RepeatAlways();
  recorder.ExpectAndReturn(httpContext.Request.[token1], token1Value);
  recorder.FailWhenCalled(httpContext.Request.[token1]);
}


I'm getting an expectation error:

Method System.Web.HttpContext.get_Request() has 1 more expected calls


And the tracer shows 3 instances of System.Web.HttpRequest being mocked.

This all looks like right (except the failed statement).

What I should be doing is:

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
  recorder.ExpectAndReturn(httpContext.Request, httpRequest).RepeatAlways();
  recorder.ExpectAndReturn(httpRequest.AppRelativeCurrentExecutionFilePath, requestedPath).RepeatAlways();
  recorder.ExpectAndReturn(httpRequest.[token1], token1Value);
  recorder.FailWhenCalled(httpRequest.[token1]);
}


But I'm too lazy.

Is there a:

recorder.AlwaysMockTheSameIfCalled(httpContext.Request);


for the lazy guy like me?
asked by paulo.morgado (11k points)

4 Answers

0 votes
Paulo hi,
Im not sure i understood exactly what you mean.
Lets say that there is such an api how would the test look like.

also given that im not 100% sure what you need, try taking a look at
1) RecordExpectations.DefaultBehavior - allow you to tweak default behavior such as repeat always
2) you might consider using the MockAll Api
answered by lior (13.2k points)
0 votes
Basically is making the first code sample behave like the second without having to explicitly mock value returned by HttpContex.Request.

The normal expectation for me is that, usaually, a property of a complex type like HttpResponse always returns the same instance. So, as soon as I record an expectation for HttpContext.Request.<something> the value returned by Http.Context.Request should always be the same (RepeatAlways).

But, like I said, it's just because I'm lazy. The second code sample is perfectly acceptable.
answered by paulo.morgado (11k points)
0 votes
Pualo,
Your statements should work.
You might have found a bug with FailWhenCalled and Chained Methods.
Please try the following and see if it works.
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
  recorder.ExpectAndReturn(httpContext.Request.AppRelativeCurrentExecutionFilePath, requestedPath).RepeatAlways();
  recorder.ExpectAndReturn(httpContext.Request.[token1], token1Value);

  recorder.VerifyMode = VerifyMode.PassIfNotCalled;
  recorder.FailWhenCalled(httpContext.Request.[token1]);
}
answered by scott (32k points)
0 votes
I've changed the tests and now I can't reproduce the previous behavior.

I'll bookmark this and try the VerifyMode the next time I run into this.
answered by paulo.morgado (11k points)
...