True Fakes

Getting Started

Let's start True Fakes features overview with a description of the interface that will be used in the following examples. Suppose that we develop a simple vector graphics editor. Lets define the IShape interface that represents the geometric shape:

/// <summary>
/// Represents a shape which can be drawn in canvas.
/// </summary>
public interface IShape
{
    /// <summary>
    /// Shape name.
    /// </summary>
    string Name { get; set; }

    /// <summary>
    /// Draw shape.
    /// </summary>
    void Draw();

    /// <summary>
    /// Get shape dimensions.
    /// </summary>
    /// <param name="width">Dimension width.</param>
    /// <param name="height">Dimension height.</param>
    void GetDims(out int width, out int height);

    /// <summary>
    /// Get shape contour thickness.
    /// </summary>
    /// <returns>Shape contour thickness.</returns>
    int GetThickness();

    /// <summary>
    /// Check if this shape can select.
    /// </summary>
    /// <param name="xPosition">X position of test point to check.</param>
    /// <param name="yPosition">Y position of test point to check.</param>
    /// <returns>Returns true if this shape can select.</returns>
    bool CanSelect(int xPosition, int yPosition);

    /// <summary>
    /// Clarify shape center using initial position.
    /// </summary>
    /// <param name="xPosition">Initial and clarified X position.</param>
    /// <param name="yPosition">Initial and clarified Y position.</param>
    void ClarifyCenter(ref int xPosition, ref int yPosition);

    /// <summary>
    /// Occurs when shape is drawing.
    /// </summary>
    event EventHandler<DrawEventArgs> Drawing;

    /// <summary>
    /// Occurs after shape has drawed.
    /// </summary>
    event EventHandler Drawed;
}

When writing unit tests often necessary to isolate test class (system under test) from all its dependencies. For this there are special stub classes (fake-classes), simulating the expected behavior. In the following sections we will look at the syntax and capabilities to work with fake-class generated for the IShape interface.

Creating a Fake

All framework classes and interfaces are located in the TrueFakes namespace. So do not forget to add it to you test class: using TrueFakes;

NOTE: All the test methods where you use fake-objects must mark an attribute [TrueFake], which clean all the arrangements after test method is completed.

If you need to simulate any aspect of the dependence behavior you can create fake-object for it as follows:

var shapeFake = TrueFake.Of<IShape>();

Current True Fakes version supports fake-objects generation only from interfaces. We work hard to give you capability to generate fake-object from other types of entities (abstract, static, sealed classes etc).

For more information about all True Fakes features such as arranging, checking received calls, raising an event etc. please follow the documentation here.