Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

It would be great if Isolator had strict mocking for void methods

0 votes
As we unit test obsessed folks know, asserting that a method was not called is often equally important to the correctness of a test suite as asserting that a method was called.
 
With Isolator, fakes created with Isolate.Fake.Instance<T>(Members.MustSpecifyReturnValues) have the behavior of throwing if a non-void method call is made on that fake without first having set the method's return value with Isolate.WhenCalled(() => fake.NonVoidMethod()).WillReturn(returnValue).
 
This behavior is perfect if only non-void method calls can break your code under test.
 
For ensuring that no breaking extraneous void method calls are made, the user appears to be out of luck: no exception is thrown when a void method is called on a Members.MustSpecifyReturnValues fake (as the name "MustSpecifyReturnValues" well indicates).
 
Given this behavior, for those void methods with side effects that must not be experienced in order for my code under test to be correct, I have taken to converting such void methods to methods returning a dummy value.
 
Here is some example code of what that looks like:
 
public class Class
{
	public int NonVoidMethod()
	{
		return 0;
	}

	public void VoidMethod()
	{
	}

	public DummyReturnValue VoidMethodByAnyOtherName()
	{
		return DummyReturnValue.Value;
	}
}

public struct DummyReturnValue
{
	public static readonly DummyReturnValue Value = new DummyReturnValue();
}

[TestFixture]
public class Tests
{
	[Test]
	public void Test()
	{
		var c = Isolate.Fake.Instance<Class>(Members.MustSpecifyReturnValues);
		c.NonVoidMethod(); // Throws
		c.VoidMethod(); // Does not throw. c.VoidMethod() can be introduced into the code under test with no corresponding break in the unit tests (absent a call to WasNotCalled()).
		c.VoidMethodByAnyOtherName(); // Throws
	}
}

Hackish for sure and bound to raise questions from colleagues such as "why the hell are you returning this dummy value here?". "Oh, because Isolator only strictly mocks non-void methods." It would be great if Members.MustSpecifyAll existed in a future version of Isolator.

 

asked May 15, 2015 by NeilJustice (1,110 points)

1 Answer

0 votes

That is a good idea. We will add it to our list.
What do you think about Members.MustBeSpecified?

You will have to use that API with attention as tests might become fragile and fail even though the logic is still ok.

In the meantime, if you really want make sure that the specific API was not called, verify it explicitly

 

 
[Test]
public void Test()
{
    var c = Isolate.Fake.Instance<Class>(Members.MustSpecifyReturnValues);
        
    Isolate.Verify.WasNotCalled(()=> c.NonVoidMethod());
}
 
answered May 15, 2015 by eli (5,770 points)
Many thanks Eli. Members.MustBeSpecified sounds great.

Hi Neil,

You can see we've added the Members.MustBeSpecified  to our API.

We noticed you were a very active user and we haven't heard from you in a while.
I was wondering, if you are still using our product or if I can assist you in any way?
Let me know, would be great to have you back.
 
...