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
Hi,

I did a search, and didn't see anything recent about ordered mocks. Does TypeMock support these yet?

Also, I'd like to be able to specify a bunch of function calls to be stubbed out by default. For example, if I'm testing some particular part of a function, I'd like the test to only have the relevant record calls for the part I'm testing, and have the rest of the calls in that function stubbed out automatically.

using (RecordExpectations recorder = SiegeRecorderManager.StartRecording())
{
    SetUpDefaultStubs();

    // Expect some specific function calls to do with this particular test
}


Of course I can't do this at the moment since the SetUpDefaultStubs() call will be mocked.

This is pretty important since it makes the tests much smaller, and the code you see in the record block is just the code relevant to that particular test. The behaviour I'd really like is that if a function was specifically recorded in the record block, then those settings (return values etc.) are used, otherwise it is just stubbed out.

Is there any way to achieve this behaviour in the current version of TypeMock? If not, is there any chance of being able to do this in the future?

Thanks,

Rory.[/code]
________
buy extreme vaporizer
asked by rorydriscoll (3.8k points)

4 Answers

0 votes
Never mind about the second part. I managed to achieve this using the reflection mocks combined with the natural mocks.

The ordered mocks question still stands though!
________
Ford do Brasil picture
answered by rorydriscoll (3.8k points)
0 votes
Hi Rory,

Several question here ill try to answer them all.
1) Ordered mocks - actually this feature is quite high in our list and I'm hoping it wont be too long before we reach it. Currently, however, we do not support this.

2) Setup default stubs - try this section in our guide "Building a Mock Infrastructure". I think you might find it useful. If that doesn't help, you can take the SetUpDefaultStubs out from the recording block, and do a separate recording block inside recording what you need.

3) "Stub Rest" - what we thought about doing is something that will enable you to stub out all calls to a given class (using a single API).
Something in the spirit of:
MockManager.MockAllCalls(typeof(someClass)); - stub all calls on a given type 
mock.MockAllClass(); - stub all calls on a given instance.

If I'm getting you right you wants something a little different:
Mock.MockRestOfCalls(); - use existing expectation and stub out the rest.

If thats true let me know. I think it shouldn't be too hard adding this, once we implement such a feature.
answered by lior (13.2k points)
0 votes
I've just replied to my other post which touches on these similar issues. Just to follow up on this one:

1) Great! The sooner the better!

2) Thanks for pointing this out. The recording blocks look fairly similar to what I'd like to achieve, but they're pretty verbose.

3) Really I'd like to use something like MockAllClass() just to ensure that the real functions never get called, then to be able to set up overriding required calls for the current test (see my other post). My only issue with an all encompassing MockAllClass function is that often I may require specific return values from some of the functions in order for the test to proceed correctly. As I understand it, MockAllClass would have to return a default (0 or null) for each of these functions, is that correct?

Cheers,

Rory.
________
List of Chrysler transmissions
answered by rorydriscoll (3.8k points)
0 votes
:arrow: here is a way that you can do this in reflective mocks:

public Mock Stub<Type>()
{
    Mock mock = MockManager.Mock<Type>();
    foreach (MethodInfo method in mock.MockedType.GetMethods())
    {
        // don't mock mscorlib methods
        if (method.DeclaringType.Assembly == typeof(object).Assembly)
            continue;

        // mock all voids
        if (method.ReturnType == typeof(void))
        {
            mock.ExpectAlways(method.Name);
        }
        else
        // return default values
        {
            mock.AlwaysReturn(method.Name,
                              method.ReturnType.IsValueType ? Activator.CreateInstance(method.ReturnType) : null);
        }
    }
    return mock;
}


:idea: using AlwaysReturn will set the default for all methods but this will allow you to override them, and they won't fail if not called

example:
[Test,VerifyMocks] 
public void SampleTest() 
{ 
   Mock myStub = Stub<TypeToMock>(); // see method at top of this post

   // now we can set some other behavior
   myStub.ExpectAndReturn("MyMethod", 5);
}
answered by scott (32k points)
...