A different approach to Mock Verification
Scott Bellware has posted about the awkwardness of mock testing - I find that the syntaxes for mock frameworks feel awkward to me.
It is true that the Validation of calls and arguments are hidden in the setup part of the test and the verification is run automatically at the end of the test. Here is how you can have a more explicit validation using TypeMock. (Note: I am using the Generic Syntax of our coming version)
Implicit Validation
[Test] public void SomeTest() { // mock next instance of TestedClass this happens in SomeObject.DoYourThing(); Mock m = Mock<TestedClass>.MockNextInstance(); // mock foo once m.ExpectAndReturn("foo", "yada yada"); // do our thing SomeObject test = new SomeObject(); test.DoYourThing(); // verify that foo was called once m.Verify(); }
Explicit Validation
[Test] public void SomeTest() { // mock next instance of TestedClass this happens in SomeObject.DoYourThing(); Mock m = Mock<TestedClass>.MockNextInstance(); // mock foo m.AlwaysReturn("foo", "yada yada"); // do our thing SomeObject test = new SomeObject(); test.DoYourThing(); // verify that foo was called once Assert.AreEqual(1, m.GetCallCount("foo")); }
There are 2 differences:
1. Using AlwaysReturn instead of ExpectAndReturn, this turns the mock into a stub.
2. Fetching and Asserting the amount of calls made, after the test for our verification.
Notice how using a remark solves Scott’s problem altogether
The downfall for using the explicit syntax are:
1. No Fast Fail – Using the explicit syntax we will not fail fast when foo was called twice (or with wrong arguments) that could lead to another failure in the test (as the code continued to run)
2. Concurrent Tests, We can’t use the concurrent features of Mocking (using VerifyWithTimeout) that enables testing multithreaded code without playing with joining threads (just wait for a particular method in one thread to be called to signal that we can validate some data in the test thread)
2 Comments to “A different approach to Mock Verification”
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

I think the challenge here is that the method name is represented as a string. If there’s no alternative to this then it would be an annoying hindrance to refactoring.
True,
Although that is not the only challenge.
Having void methods and validating arguments are another.