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
I've been using Moq, and I want to create a mock object that forbids any calls other than those I have specified. I can't find a way to do this in TypeMock other than to verify that every method was not called.

In Moq, I do this with:
mock = new Mock<IMyInterface>(MockBehavior.Strict);
mock.VerifyAll();

In TypeMock, the closest I can find is this:
// MustSpecifyReturnValues does what I want, except for void methods
Isolate.Fake.Instance<IMyInterface>(Members.MustSpecifyReturnValues);

// Do this for all void methods
Isolate.Verify.WasNotCalled(...);
Isolate.Verify.WasNotCalled(...);
Isolate.Verify.WasNotCalled(...);
Isolate.Verify.WasNotCalled(...);

Is there a way to do this without explicitly checking each method? Or a way to make the default implementation for all the mocked methods throw?
asked by MobyDisk (3.6k points)

3 Answers

0 votes
Currently Isolator AAA does not have VerifyAll you can use the older API (reflective/natural mocks) that do have this functionality. or use reflection to set expectation using Isolate.NonPublic to set all other methods to throw exception if called.
var someClass = new ClassUnderTest();
var type = someClass.GetType();
foreach(var method in type.GetMethods())
{
    var methodName = method.Name;
    Isolate.NonPublic.WhenCalled(someClass, methodName)
        .WillThrow(new Exception(string.Format("Method {0} was called", methodName)));
}

We have noticed that setting such behavior seems creates very fragile tests, is there a specific test you want to write that requires to make sure that most of an object methods were not called?
answered by dhelper (11.9k points)
0 votes
We have noticed that setting such behavior seems creates very fragile tests, is there a specific test you want to write that requires to make sure that most of an object methods were not called?

I understand how this might be bad in general. In this specific case, I have a plug-in interface with a sequence diagram explaining what sequence of calls are made to the plug-ins, and in what order. I am verifying that my code calls that interface as specified.
answered by MobyDisk (3.6k points)
0 votes
We have noticed that setting such behavior seems creates very fragile tests, is there a specific test you want to write that requires to make sure that most of an object methods were not called?


This is probably egregious abuse on my part, but I was using typemock to mock an object which had an internal memory buffer and read/write methods much like a binaryreader/writer.

I'd intercepted most of the write calls so that they would write to a MemoryStream, but accidentally missed one. Because they returned void I wasn't made aware of this.

It took me a while to realise I had missed it :)
answered by adante (1.7k points)
...