home | software | mockgen | example
Interface source | MockGen output |
The namespace of the tool is included, as the generated code uses support classes. | |
using System; |
using System; using Drew.Tools.MockGen; |
The mock class is created in a sub-namespace, 'Tests'. A brief comment indicates the origin for code-generation. | |
namespace MockGenTest { |
// mock generated from: C:\Documents and Settings\Drew\My Documents\Visual Studio Projects\MockGenTest\IStockRoom.cs namespace MockGenTest.Tests { |
A concrete class that derives from the interface being mocked is created. 'Mock' is prepended to the interface name, with any leading 'I' is removed. Note that comments are removed in this version (later versions may retain comments). | |
/// <summary> /// Interface of a stock room. /// </summary> public interface IStockRoom { |
public class MockStockRoom : IStockRoom { |
Each event on the interface is implemented with a corresponding 'Fire' method. Call this method to force the mock to raise its event. | |
event StockEventHandler StockRemoved; |
public event StockEventHandler StockRemoved; public void Fire_StockRemoved(StockEventArgs e) { StockRemoved(this, e); } |
For methods, additional public fields allow staging of return values, delays in the methods return (useful when testing threaded scenarios), exceptions to be raised, and assertions to be made regarding how many other classes called, and what values they provided. Parameter modifiers (out, params, ref) are also supported. | |
bool IsInStock( Product product, int numberOfUnits); |
public int IsInStock_CallCount = 0; <---- the number of times this method has been called public long IsInStock_CallSequence = 0; <---- sequence number of the most recent call to this method public int IsInStock_SleepMillis = 0; <---- period of time the method should sleep when called (optional) public System.Exception IsInStock_Exception; <---- an exception to be raised when the method is called (optional) public Product IsInStock_product; <---- value of parameter 'product' passed in the last invocation public System.Collections.ArrayList IsInStock_product_List = new System.Collections.ArrayList(); <---- list of all values passed for the parameter 'product' public int IsInStock_numberOfUnits; <---- value of parameter 'numberOfUnits' passed in the last invocation public System.Collections.ArrayList IsInStock_numberOfUnits_List = new System.Collections.ArrayList(); <---- list of all values passed for the parameter 'numberOfUnits' public bool IsInStock_ReturnValue; <---- value to be returned for all invocations of this method public System.Collections.Queue IsInStock_ReturnValueQueue = new System.Collections.Queue(); <---- a queue of values to be returned, one per call public bool IsInStock(Product product, int numberOfUnits) { IsInStock_CallCount++; IsInStock_CallSequence = MockSequence.Next(); IsInStock_product = product; IsInStock_product_List.Add(product); IsInStock_numberOfUnits = numberOfUnits; IsInStock_numberOfUnits_List.Add(numberOfUnits); if (IsInStock_SleepMillis>0) System.Threading.Thread.Sleep(IsInStock_SleepMillis); if (IsInStock_Exception!=null) throw IsInStock_Exception; if (IsInStock_ReturnValueQueue.Count==0) return IsInStock_ReturnValue; else return (bool)IsInStock_ReturnValueQueue.Dequeue(); } |
Properties are always mocked with a getter and setter, allowing tests to stage values, or read those which are set. A call count, and sequence number are stored for whichever of the getter/setter are on the interface. | |
StockRoomStatus Status { get; } |
private StockRoomStatus _status; public int Status_Get_CallCount = 0; <---- the number of times getter has been called public long Status_Get_CallSequence = -1; <---- the sequence number of the most recent getter call public StockRoomStatus Status { get { Status_Get_CallCount++; Status_Get_CallSequence = MockSequence.Next(); return _status; } set <---- setter is added, so tests can stage the property value { _status = value; } } |
} |
} |
Any delegate defined within the interface source will have a corresponding event counter
class created. This class is extremely useful for monitoring events from within tests -- no additional handler-method
nor fields (to store args, etc) on your test class need be introduced. The class is inteded to be used as such:
| |
public delegate void StockEventHandler( object sender, StockEventArgs e); |
public class StockEventCounter { public int Count = 0; <---- number of times event has been raised public object LastSender; <---- the most recent sender parameter public StockEventArgs LastArgs; <---- the most recent event args public void Handler(object sender, StockEventArgs e) { Count++; LastSender = sender; LastArgs = e; } } |
} |
} |