How to Stub with Conditional Expectations
Conditional Expectations is quite a powerful tool, that allows expectations to act differently according to the arguments passed to the mocked method.
This is very useful for Stubbing methods. Suppose there is a static method Customer.GetAge(string customerName) that needs to be isolated. But the tested code calls GetAge several times with different customerNames.
How can Mocked Customer.GetAge be mocked, to return a different age for each customerName? You guessed correctly Conditional Expectations.
Here is how we can set up GetAge() to return 27 for “Adam” and 22 for “Eve”.
Mock customerMock = MockManager.Mock(typeof(Customer)); customerMock.AlwaysReturn("GetAge",27).When("Adam"); customerMock.AlwaysReturn("GetAge",22).When("Eve");
Note the usage of When(). This is how Conditional Expectations are invoked. In the above example TypeMock will always return 27 when Customer.GetAge(“Adam”) is called and 22 when Customer.GetAge(“Eve”) is called.
In Natural Mocks our set up will look like this
using (RecordExpectations recorder = RecorderManager.StartRecording()) { Customer.GetAge("Adam"); recorder.Return(27).RepeatAlways.WhenArgumentsMatch(); Customer.GetAge("Eve"); recorder.Return(22).RepeatAlways.WhenArgumentsMatch(); }
You can probably see how using Conditional Expectation will help simplify mocking.
Conclusion
Here are the different ways that expectations can be setup in TypeMock.
- Always Expectations:
Methods mocked with Always will always be mocked, but are not verified (i.e. the method can be called zero or more times) - Invocation Expectations:
These methods are verified, and must be called for the test to pass - Conditional Always Expectation:
These Methods will not be verified and will be mocked ONLY when the arguments match those expected. (i.e. the method can be called zero or more times when the arguments match) - Conditional Invocation Expectation
These Methods are verified and must be called with the correct arguments for the test to pass.
The priorities are 4-3-2-1. So if there is both a Conditional Invocation Expectation (4) and an Invocation Expectations (2), the Conditional Invocation Expectation (4) will be used.
Recent Posts
- 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
- Goal-driven Development
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- 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
