How Dynamic Mock Objects can Auto Pilot
When creating a Mock Object from an interface, the object is set in Strict mode. This means that any unexpected call to the object will fail.
The reason is quite simple. It is because there is no real implementation of any methods.
Since version 3.6.1 Dynamic Mock Objects will have a real default implementation. The implementation will return default values when ever a method is called and will build properties to act as simple properties (after setting a property, you will be able to read it).
To enable this feature you will have to turn off the strictness of these mocks.
Here is an example interface
public interface IImplement { int MyProperty { get; set; } }
Here is test that shows the auto pilot property works.
Natural Mocks:
[Test] public void AutoPilotProperties() { IImplement mockImpl = (IImplement)RecorderManager.CreateMockedObject( typeof(IImplement), StrictFlags.ArbitraryMethodsAllowed); mockImpl.MyProperty = 10; Assert.AreEqual(10, mockImpl.MyProperty); }
Reflective mocks:
[Test] public void AutoPilotProperties() { MockObject mock = MockManager.MockObject(typeof(IImplement)); IImplement mockImpl = mock.Object as IImplement; mock.Strict = false; mockImpl.MyProperty = 10; Assert.AreEqual(10, mockImpl.MyProperty); }










One Response to “How Dynamic Mock Objects can Auto Pilot”
1 Eli Lopian’s Blog (TypeMock) » Blog Archive » New and Noteworthy - Version 3.6.1 24 August 2007 @ 9:17 pm
[…] TypeMock.NET now creates a default implementation for interfaces and abstract methods. This allows natural property behavior and non strict method calls […]
Leave a Reply