When to MockObject?
In my previous post I showed how to mock future instances. Now we will see when to use MockObject.
What is the difference?
The difference is quite simple. But before we delve into it we should talk about the 2 objects that are associated with each mock.
Each mock has a controller that controls the behavior. This is done using the Mock type. Here we setup the Expectations and Validations. And each mock has the actual instance that is being mocked.
Using MockManager.Mock will mock a future instance.
Using MockManager.MockObject will immediately create a mocked instance. It is possible to access this instance using the mock.Object property. (Other mocking frameworks have only this option)
After creating a MockObject we can then pass this object as an argument to our tested method to inject the Mock.
[Test] public void MockObjectTest() { MockObject controlListener = MockManager.MockObject(typeof(MyListener)); MyListener mockedListener = (MyListener)controlListener.Object; // Setup expectation // inject our mocked object into our test Messages.Broadcast("I am alive",mockedListener); // Assertions... }
Note: There is no need for MyListener to be an interface, it could even be a sealed class!
What about interfaces?
When the mocked type (MyListener) is an interface or an abstract class TypeMock will create a new Type on-the-fly, that extends the mocked interface. This new object can then be passed to our tested code.
Mocks returning Mocks
A mocked expectation can return another mock. Here is an example
public class MyFactory { public MyListener GetListener() { // Get Listener through some complex external services } }
We can now setup the expectations that GetListener() will return a mocked MyListener.
[Test] public void MockObjectTest() { // mock future instance of MyFactory Mock factory = MockManager.Mock(typeof(MyFactory)); // create a MockObject of MyListener MockObject controlListener = MockManager.MockObject(typeof(MyListener)); MyListener mockedListener = (MyListener)controlListener.Object; // Setup expectation factory.ExpectAndReturn("GetListener",mockedListener); }
Summary
Here is a list of places where you should use MockObject.
- When a mocked object is passed to the tested method.
- When you need to mock an abstract class and interface.
- When a mocked method returns a mocked type
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
