New Best Practice Pattern
In our next release TypeMock.NET will enhance the Best Practices to use Mocks and we have added 2 new ways to use mocks
- Scoping the Mocks
- Using Test Attributes
Scoping the Mocks
In this way the mocks are scoped using the following pattern
using (new MockScope()) // can set an optional timeout { // setup mocks // run tests // optional assert Assert.That(); } // mocks will be verified
The advantages of using this method are:
- No dangling mocks will be left between tests.
- All Expectations will be verified.
The disadvantage is:
- Unverified mocks will take precedence and hide the Assert Failure.
Using Test Attributes*
We have added an even better way to use mocks, by defining TypeMock custom attributes to decorate the test methods.
[Test] [VerifyMocks(Timeout=150)] // mocks are always verified public void TestVerificationWithTimeout() { // setup mocks // run tests // optional assert Assert.That(): }
Once we put the [VerifyMocks] attribute on the test, the test will automatically verify all mocks.
The advantages of using this method are:
- No dangling mocks will be left between tests.
- All Expectations will be verified.
- Asserts will take precedence over mock verification.
- Mock will be cleared in all cases (even if a failure was thrown)
It is possible to use [VerifyMocks(Timeout=100)] that will fail the tests if not all expectations where met within the timeout.
There will also be a [ClearMocks] attribute that will always clear the mocks between tests (without verification)
These attributes can be used on the test class level too so the following will verify mocks after each test
[TestFixture] [VerifyMocks] public class TestsThatUseTypeMock { }
This will clear mock expectations after each test
[TestFixture] [ClearMocks] public class TestsTheUseTypeMock_and_ExplicitlyVerify { }
Oh, and if you are asking what testing frameworks are supported, I will surprise you and tell you that all frameworks are supported!!
*Note: The Test Attributes will require a licensed edition.
2 Comments to “New Best Practice Pattern”
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

[...] If you have read my previous post about the TypeMock Test Attributes you probably have noticed that the attributes are test framework agnostic! [...]
[...] The new TypeMock Decorators. [...]