FastMoq
Search Results for

    Show / Hide Table of Contents

    Class MockerCreationExtensions

    Helpers for creating components or raw mocks when you need constructor selection to be explicit in a test.

    Inheritance
    object
    MockerCreationExtensions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: FastMoq.Extensions
    Assembly: FastMoq.Core.dll
    Syntax
    public static class MockerCreationExtensions
    Examples

    Use these overloads when the constructor choice matters and you want to supply only a subset of arguments explicitly.

    var mocker = new Mocker();
    

    var processor = mocker.CreateInstance<OrderProcessor, string>( new Dictionary<Type, object?> { [typeof(string)] = "orders-eu" });

    processor.Should().NotBeNull();

    Methods

    CreateInstance<T, TParam1>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)

    Creates an instance of T by selecting the constructor that matches TParam1 and applying the supplied creation flags.

    Declaration
    public static T? CreateInstance<T, TParam1>(this Mocker mocker, InstanceCreationFlags flags, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The FastMoq container used to resolve the remaining constructor dependencies.

    InstanceCreationFlags flags

    The constructor-selection and optional-parameter behavior overrides to apply for this creation request.

    Dictionary<Type, object> data

    A map of constructor parameter types to explicit values that should be injected when creating the instance.

    Returns
    Type Description
    T

    The created instance, or null when resolution cannot produce one.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The explicitly supplied constructor parameter type used to identify the target constructor.

    CreateInstance<T, TParam1>(Mocker, Dictionary<Type, object?>)

    Creates an instance of T by selecting the constructor that matches TParam1 and injecting the supplied value from data.

    Declaration
    public static T? CreateInstance<T, TParam1>(this Mocker mocker, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Dictionary<Type, object> data

    The data.

    Returns
    Type Description
    T

    The created instance or null when resolution cannot produce one.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The type of the t param1.

    Examples

    This is useful when a component has multiple constructors and your test needs to target the overload that accepts a tenant or region value.

    var mocker = new Mocker();
    

    var handler = mocker.CreateInstance<InvoiceHandler, string>( new Dictionary<Type, object?> { [typeof(string)] = "contoso" });

    handler.Should().NotBeNull(); handler!.TenantName.Should().Be("contoso");

    CreateInstance<T, TParam1, TParam2>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)

    Creates an instance of T by targeting the constructor that matches TParam1 and TParam2, while honoring the supplied creation flags.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2>(this Mocker mocker, InstanceCreationFlags flags, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The FastMoq container used to resolve remaining constructor dependencies.

    InstanceCreationFlags flags

    The constructor-selection and optional-parameter behavior overrides to apply for this call.

    Dictionary<Type, object> data

    A map of explicit constructor argument types to values.

    Returns
    Type Description
    T

    The created instance, or null when construction fails.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The first constructor parameter type used to identify the target constructor.

    TParam2

    The second constructor parameter type used to identify the target constructor.

    CreateInstance<T, TParam1, TParam2>(Mocker, Dictionary<Type, object?>)

    Creates an instance of T. Parameter data allows matching of constructors by type and uses those values in the creation of the instance.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2>(this Mocker mocker, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Dictionary<Type, object> data

    The data.

    Returns
    Type Description
    T

    T.

    Type Parameters
    Name Description
    T
    TParam1

    The type of the t param1.

    TParam2

    The type of the t param2.

    CreateInstance<T, TParam1, TParam2, TParam3>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)

    Creates an instance of T by targeting the constructor that matches three explicit parameter types and applying the supplied creation flags.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3>(this Mocker mocker, InstanceCreationFlags flags, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The FastMoq container used to resolve remaining constructor dependencies.

    InstanceCreationFlags flags

    The constructor-selection and optional-parameter behavior overrides to apply for this call.

    Dictionary<Type, object> data

    A map of explicit constructor argument types to values.

    Returns
    Type Description
    T

    The created instance, or null when construction fails.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The first constructor parameter type used to identify the target constructor.

    TParam2

    The second constructor parameter type used to identify the target constructor.

    TParam3

    The third constructor parameter type used to identify the target constructor.

    CreateInstance<T, TParam1, TParam2, TParam3>(Mocker, Dictionary<Type, object?>)

    Creates an instance of T. Parameter data allows matching of constructors by type and uses those values in the creation of the instance.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3>(this Mocker mocker, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Dictionary<Type, object> data

    The data.

    Returns
    Type Description
    T

    T.

    Type Parameters
    Name Description
    T
    TParam1

    The type of the t param1.

    TParam2

    The type of the t param2.

    TParam3

    The type of the t param3.

    CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)

    Creates an instance of T by targeting the constructor that matches four explicit parameter types and applying the supplied creation flags.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(this Mocker mocker, InstanceCreationFlags flags, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The FastMoq container used to resolve remaining constructor dependencies.

    InstanceCreationFlags flags

    The constructor-selection and optional-parameter behavior overrides to apply for this call.

    Dictionary<Type, object> data

    A map of explicit constructor argument types to values.

    Returns
    Type Description
    T

    The created instance, or null when construction fails.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The first constructor parameter type used to identify the target constructor.

    TParam2

    The second constructor parameter type used to identify the target constructor.

    TParam3

    The third constructor parameter type used to identify the target constructor.

    TParam4

    The fourth constructor parameter type used to identify the target constructor.

    CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(Mocker, Dictionary<Type, object?>)

    Creates an instance of T. Parameter data allows matching of constructors by type and uses those values in the creation of the instance.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(this Mocker mocker, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Dictionary<Type, object> data

    The data.

    Returns
    Type Description
    T

    T.

    Type Parameters
    Name Description
    T
    TParam1

    The type of the t param1.

    TParam2

    The type of the t param2.

    TParam3

    The type of the t param3.

    TParam4

    The type of the t param4.

    CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)

    Creates an instance of T by targeting the constructor that matches five explicit parameter types and applying the supplied creation flags.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(this Mocker mocker, InstanceCreationFlags flags, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The FastMoq container used to resolve remaining constructor dependencies.

    InstanceCreationFlags flags

    The constructor-selection and optional-parameter behavior overrides to apply for this call.

    Dictionary<Type, object> data

    A map of explicit constructor argument types to values.

    Returns
    Type Description
    T

    The created instance, or null when construction fails.

    Type Parameters
    Name Description
    T

    The concrete type to create.

    TParam1

    The first constructor parameter type used to identify the target constructor.

    TParam2

    The second constructor parameter type used to identify the target constructor.

    TParam3

    The third constructor parameter type used to identify the target constructor.

    TParam4

    The fourth constructor parameter type used to identify the target constructor.

    TParam5

    The fifth constructor parameter type used to identify the target constructor.

    CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(Mocker, Dictionary<Type, object?>)

    Creates an instance of T. Parameter data allows matching of constructors by type and uses those values in the creation of the instance.

    Declaration
    public static T? CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(this Mocker mocker, Dictionary<Type, object?> data) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Dictionary<Type, object> data

    The data.

    Returns
    Type Description
    T

    T.

    Type Parameters
    Name Description
    T
    TParam1

    The type of the t param1.

    TParam2

    The type of the t param2.

    TParam3

    The type of the t param3.

    TParam4

    The type of the t param4.

    TParam5

    The type of the t param5.

    CreateMockInternal(Mocker, Type, IReadOnlyCollection<object?>?, bool, bool)

    Creates the mock internal.

    Declaration
    public static Mock CreateMockInternal(this Mocker mocker, Type type, IReadOnlyCollection<object?>? parameterList = null, bool isNonPublic = false, bool setupMock = true)
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Type type

    The type.

    IReadOnlyCollection<object> parameterList

    The constructor parameters.

    bool isNonPublic

    if set to true, indicates if non-public constructors should be searched.

    bool setupMock

    if set to true, attempts to setup internal mocks and properties.

    Returns
    Type Description
    Mock
    Remarks

    Parameter list only works if the type is concrete. Otherwise, pass an empty list.

    Exceptions
    Type Condition
    ApplicationException

    Cannot create instance of Mock.

    CreateMockInternal<T>(Mocker, bool)

    Creates a mock given the Type of Mock. Properties will be stubbed and have default setups.

    Declaration
    public static Mock<T> CreateMockInternal<T>(this Mocker mocker, bool isNonPublic = true) where T : class
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    bool isNonPublic

    if set to true, indicates if non-public constructors should be searched.

    Returns
    Type Description
    Mock<T>
    Type Parameters
    Name Description
    T

    Type of Mock

    Remarks

    This is designed for interface mocks or concrete mocks without parameters.

    Exceptions
    Type Condition
    ApplicationException

    Cannot create instance of Mock.

    SetupMockProperty<TMock>(Mock<TMock>, Expression<Func<TMock, object>>, object)

    Sets up a readable property on a mock by using a strongly typed property expression.

    Declaration
    public static void SetupMockProperty<TMock>(this Mock<TMock> mock, Expression<Func<TMock, object>> propertyExpression, object value) where TMock : class
    Parameters
    Type Name Description
    Mock<TMock> mock

    The mock whose property getter should be configured.

    Expression<Func<TMock, object>> propertyExpression

    An expression that selects the property to configure.

    object value

    The value the property getter should return.

    Type Parameters
    Name Description
    TMock

    The mocked type that exposes the property.

    SetupMockProperty<TMock>(Mock<TMock>, PropertyInfo, object)

    Setups the mock for given property info.

    Declaration
    public static void SetupMockProperty<TMock>(this Mock<TMock> mock, PropertyInfo propertyInfo, object value) where TMock : class
    Parameters
    Type Name Description
    Mock<TMock> mock

    The mock.

    PropertyInfo propertyInfo

    The property information.

    object value

    The value.

    Type Parameters
    Name Description
    TMock

    The type of mock.

    SetupMockProperty<TMock>(Mock<TMock>, string, object)

    Setups the mock for given property name.

    Declaration
    public static void SetupMockProperty<TMock>(this Mock<TMock> mock, string propertyName, object value) where TMock : class
    Parameters
    Type Name Description
    Mock<TMock> mock

    The mock.

    string propertyName

    Name of the property.

    object value

    The value.

    Type Parameters
    Name Description
    TMock

    The type of the t mock.

    In this article
    Back to top
    Generated 2026-04-29 03:53 UTC