Oct
31

How Dynamic Mock Objects can Auto Pilot

Author Eli Lopian    Category .NET Tests     Tags

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); }
Bookmark and Share

4 Comments to “How Dynamic Mock Objects can Auto Pilot”

Post comment