Class MockerCreationExtensions
Helpers for creating components or raw mocks when you need constructor selection to be explicit in a test.
Inherited Members
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?>)
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 | |
| InstanceCreationFlags | flags | |
| Dictionary<Type, object> | data |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TParam1 |
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?>)
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 | |
| InstanceCreationFlags | flags | |
| Dictionary<Type, object> | data |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TParam1 | |
| TParam2 |
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?>)
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 | |
| InstanceCreationFlags | flags | |
| Dictionary<Type, object> | data |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TParam1 | |
| TParam2 | |
| TParam3 |
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?>)
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 | |
| InstanceCreationFlags | flags | |
| Dictionary<Type, object> | data |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TParam1 | |
| TParam2 | |
| TParam3 | |
| TParam4 |
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?>)
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 | |
| InstanceCreationFlags | flags | |
| Dictionary<Type, object> | data |
Returns
| Type | Description |
|---|---|
| T |
Type Parameters
| Name | Description |
|---|---|
| T | |
| TParam1 | |
| TParam2 | |
| TParam3 | |
| TParam4 | |
| TParam5 |
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 |
| bool | setupMock | if set to |
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 |
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)
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 | |
| Expression<Func<TMock, object>> | propertyExpression | |
| object | value |
Type Parameters
| Name | Description |
|---|---|
| TMock |
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. |