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
0 votes
We're trying to determine how to standardize our coding here and there are two seperate sets of thought:

1) always use Recorder.ExpectAndReturn. It keeps the code cleaner, the test is in one line, etc.

I.E.

recorder.ExpectAndReturn(HttpContext.Current, mockedContext);

2) Use the methodology of mock the call then return the expected item (if there is one)

I.E.

HttpContext dummy = HttpContext.Current;
recorder.return(dummy);

I ran into a problem today where I was trying to mock a call to cache insert, even though it does not return a value, and the only way I could see to do it (thanks to a coworker) was to use

mockCache.Insert("",null,DateTime.Now) (since the call takes those params)

This satisfies the mock, but there is no equivilent recorder.CallVoid(blah)

So what is the preferred method of returning values/void calls from the TypeMock development team? We are having a meeting on this tomorrow so any responses are welcome.

Thanks!
asked by JEberlin (1.7k points)

4 Answers

0 votes
Hi
Actually your coworker gave you the right answer.
There is no API for calling void method and the reason is simple:
When you are calling void method you usually want to do two things:
1. Mock the call for the method.
2. Set expectation for it.

This is done automatically by calling any method inside the recording block.
Less writing = Less mistakes :)

:idea: Note: Exception for the rule above is when you do:
recorder.ExpectAndReturn(MyClass.MyMethod(), new MyReurnObject())

where the new MyReurnObject() will create a real object!

Hope it helps.
answered by ohad (35.4k points)
0 votes
But as a general rule which form do you suggest using?

Recorder.ExpectAndReturn(HttpContext,mockedContext)

or

HttpContext dummy = mockedContext;
recorder.Return(mockedContext)
answered by JEberlin (1.7k points)
0 votes
Hi
There is no difference at all between the two options.
Most users I've seen use ExpectAndReturn
I guess its because is the same thing in one line and its less writing.
You should choose whatever seems to your team clearer to read and use.
answered by ohad (35.4k points)
0 votes
The two ways are used so that all methods can be written.

void method must use the independent way:
   VoidMethod();
   recorder.Throw(...);

there is no: recorder.MockVoid(...); (this is because the c# compiler cannot send a void method as an argument.)

Other statements can use both ways, so each team can choose what suit them the best.

{  
   VoidMethod();
       recorder.Throw(...);
   int dummy = MyMethod(1);
       recorder.Return(5).WhenArgumentsMatch();
   recorder.ExpectAndReturn(Property,5);
}
answered by scott (32k points)
...