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.