FastMoq
Search Results for

    Show / Hide Table of Contents

    Class Mocker

    Core mock coordinator (provider-based creation integrated).

    Inheritance
    object
    Mocker
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: FastMoq
    Assembly: FastMoq.Core.dll
    Syntax
    public class Mocker
    Remarks

    Resolution Hierarchy (v4)

    FastMoq uses consistent resolution hierarchies across different APIs:

    GetObject<T>() - Dependency Injection (Property/Field/Constructor)

    1. Custom registration via AddType<T>() or AddKnownType<T>()
    2. Built-in known instances (e.g., IFileSystem returns in-memory file system)
    3. Tracked mocks via GetOrCreateFastMock (get-or-create paradigm)
    4. Default value (null or type default)

    GetObject respects all custom configurations and auto-creates mocks when needed.

    GetMock<T>() / GetOrCreateMock<T>() - Test Setup (Mock-Only Path)

    1. Tracked mocks only (get-or-create tracked mock, no custom registrations applied)
    2. Mocks are preconfigured via SetupFastMock (e.g., IFileSystem delegates to built-in)

    GetMock/GetOrCreateMock always return tracked mocks for test setup, bypassing custom AddType registrations. This is intentional: AddType is for runtime instances, while GetMock is for test mocks. For setup values, use AddMock() or component constructor parameters instead.

    GetParameter() - Constructor Parameter Resolution

    1. Custom type mapping (AddType<TInterface, TImpl>())
    2. Falls back to GetObject for all other resolution

    Constructor parameters follow the same hierarchy as GetObject after type mapping.

    Examples

    Use Mocker as the main test composition root when you want provider-first arrangement based on concrete registrations and policy-driven construction.

    var fileSystem = new MockFileSystem();
    fileSystem.AddFile(@"c:\orders\42.json", new MockFileData("{ \"id\": 42, \"total\": 125.50 }"));
    

    var mocker = new Mocker() .AddType<IFileSystem>(fileSystem);

    var resolvedFileSystem = mocker.GetRequiredObject<IFileSystem>();

    resolvedFileSystem.File.Exists(@"c:\orders\42.json").Should().BeTrue(); resolvedFileSystem.Should().BeSameAs(fileSystem);

    Constructors

    Mocker()

    Initializes a new Mocker instance with a no-op external logging callback.

    Declaration
    public Mocker()

    Mocker(Action<LogLevel, EventId, string, Exception?>)

    Initializes a new Mocker instance with an exception-aware logging callback.

    Declaration
    public Mocker(Action<LogLevel, EventId, string, Exception?> loggingCallback)
    Parameters
    Type Name Description
    Action<LogLevel, EventId, string, Exception> loggingCallback

    Callback invoked for log entries emitted by this mocker.

    Mocker(Action<LogLevel, EventId, string>)

    Initializes a new Mocker instance with a three-parameter logging callback.

    Declaration
    public Mocker(Action<LogLevel, EventId, string> loggingCallback)
    Parameters
    Type Name Description
    Action<LogLevel, EventId, string> loggingCallback

    Callback invoked for log entries emitted by this mocker.

    Mocker(Dictionary<Type, IInstanceModel>)

    Initializes a new Mocker instance with an existing type map.

    Declaration
    public Mocker(Dictionary<Type, IInstanceModel> map)
    Parameters
    Type Name Description
    Dictionary<Type, IInstanceModel> map

    The preconfigured type registrations to seed into the mocker.

    Mocker(Dictionary<Type, IInstanceModel>, Action<LogLevel, EventId, string, Exception?>)

    Initializes a new Mocker instance with an existing type map and an exception-aware logging callback.

    Declaration
    public Mocker(Dictionary<Type, IInstanceModel> map, Action<LogLevel, EventId, string, Exception?> loggingCallback)
    Parameters
    Type Name Description
    Dictionary<Type, IInstanceModel> map

    The preconfigured type registrations to seed into the mocker.

    Action<LogLevel, EventId, string, Exception> loggingCallback

    Callback invoked for log entries emitted by this mocker.

    Mocker(Dictionary<Type, IInstanceModel>, Action<LogLevel, EventId, string>)

    Initializes a new Mocker instance with an existing type map and a three-parameter logging callback.

    Declaration
    public Mocker(Dictionary<Type, IInstanceModel> map, Action<LogLevel, EventId, string> loggingCallback)
    Parameters
    Type Name Description
    Dictionary<Type, IInstanceModel> map

    The preconfigured type registrations to seed into the mocker.

    Action<LogLevel, EventId, string> loggingCallback

    Callback invoked for log entries emitted by this mocker.

    Fields

    creatingTypeList

    Tracks types currently being created so recursive resolution can short-circuit circular member population.

    Declaration
    protected readonly List<Type> creatingTypeList
    Field Value
    Type Description
    List<Type>

    fileSystem

    The shared in-memory file system used by built-in file-system resolution helpers.

    Declaration
    public readonly MockFileSystem fileSystem
    Field Value
    Type Description
    MockFileSystem

    mockCollection

    Stores the tracked mock models for this Mocker instance.

    Declaration
    protected readonly List<MockModel> mockCollection
    Field Value
    Type Description
    List<MockModel>

    Properties

    Behavior

    Feature flag container controlling behavior such as base-call usage, dependency injection, nested member population, logging capture, and strict verification.

    Declaration
    public MockBehaviorOptions Behavior { get; set; }
    Property Value
    Type Description
    MockBehaviorOptions

    ConstructorHistory

    Tracks constructor-selection history for created instances.

    Declaration
    public ConstructorHistory ConstructorHistory { get; }
    Property Value
    Type Description
    ConstructorHistory

    ExceptionLog

    Captured exception messages observed during resolution and invocation helper flows.

    Declaration
    public ObservableExceptionLog ExceptionLog { get; }
    Property Value
    Type Description
    ObservableExceptionLog

    HttpClient

    The shared HttpClient created for this mocker instance.

    Declaration
    public HttpClient HttpClient { get; }
    Property Value
    Type Description
    HttpClient

    LogEntries

    Captured log entries observed through this mocker instance.

    Declaration
    public ObservableLogEntries LogEntries { get; }
    Property Value
    Type Description
    ObservableLogEntries

    LoggingCallback

    Callback invoked when FastMoq captures a log entry through this mocker instance.

    Declaration
    public Action<LogLevel, EventId, string, Exception?> LoggingCallback { get; }
    Property Value
    Type Description
    Action<LogLevel, EventId, string, Exception>

    OptionalParameterResolution

    Controls how optional constructor and invocation parameters are resolved when values are not supplied explicitly.

    Declaration
    public OptionalParameterResolutionMode OptionalParameterResolution { get; set; }
    Property Value
    Type Description
    OptionalParameterResolutionMode

    Policy

    Policy settings that control built-in type resolution and constructor fallback behavior.

    Declaration
    public MockerPolicyOptions Policy { get; }
    Property Value
    Type Description
    MockerPolicyOptions

    Uri

    The base URI associated with the shared HttpClient.

    Declaration
    public Uri Uri { get; }
    Property Value
    Type Description
    Uri

    Methods

    AddFileSystemAbstractionMapping()

    Registers the standard System.IO.Abstractions mappings backed by the current mock file system.

    Declaration
    public void AddFileSystemAbstractionMapping()

    AddInjections<T>(T, Type?)

    Populates injectable fields and properties on the supplied object using the current resolution pipeline.

    Declaration
    public T AddInjections<T>(T obj, Type? referenceType = null) where T : class?
    Parameters
    Type Name Description
    T obj

    The object whose injectable members should be populated.

    Type referenceType

    Optional type used to discover injectable members instead of the runtime type.

    Returns
    Type Description
    T

    The same object instance after injection.

    Type Parameters
    Name Description
    T

    The object type to update.

    AddKeyedType(Type, object, Type, Func<Mocker, object>?, bool, params object?[]?)

    Adds a keyed type registration for this Mocker instance.

    Declaration
    public Mocker AddKeyedType(Type tInterface, object serviceKey, Type tClass, Func<Mocker, object>? createFunc = null, bool replace = false, params object?[]? args)
    Parameters
    Type Name Description
    Type tInterface
    object serviceKey
    Type tClass
    Func<Mocker, object> createFunc
    bool replace
    object[] args
    Returns
    Type Description
    Mocker

    AddKeyedType<T>(object, Func<Mocker, T>?, bool, params object?[]?)

    Adds a keyed concrete registration with factory for this Mocker instance.

    Declaration
    public Mocker AddKeyedType<T>(object serviceKey, Func<Mocker, T>? createFunc = null, bool replace = false, params object?[]? args) where T : class
    Parameters
    Type Name Description
    object serviceKey
    Func<Mocker, T> createFunc
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    T

    AddKeyedType<T>(object, T, bool)

    Adds a keyed concrete value for this Mocker instance.

    Declaration
    public Mocker AddKeyedType<T>(object serviceKey, T value, bool replace = false)
    Parameters
    Type Name Description
    object serviceKey
    T value
    bool replace
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    T

    AddKeyedType<TInterface, TClass>(object, bool, params object?[]?)

    Adds a keyed interface-to-concrete registration for this Mocker instance.

    Declaration
    public Mocker AddKeyedType<TInterface, TClass>(object serviceKey, bool replace = false, params object?[]? args) where TInterface : class where TClass : class
    Parameters
    Type Name Description
    object serviceKey
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    TInterface
    TClass

    AddKeyedType<TInterface, TClass>(object, Func<Mocker, TClass>?, bool, params object?[]?)

    Adds a keyed interface-to-concrete registration with factory for this Mocker instance.

    Declaration
    public Mocker AddKeyedType<TInterface, TClass>(object serviceKey, Func<Mocker, TClass>? createFunc, bool replace = false, params object?[]? args) where TInterface : class where TClass : class
    Parameters
    Type Name Description
    object serviceKey
    Func<Mocker, TClass> createFunc
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    TInterface
    TClass

    AddKnownType(KnownTypeRegistration, bool)

    Adds a custom known-type registration for this Mocker instance. This is the extensibility point for framework-like types that need special resolution or setup behavior.

    Declaration
    public Mocker AddKnownType(KnownTypeRegistration registration, bool replace = false)
    Parameters
    Type Name Description
    KnownTypeRegistration registration
    bool replace
    Returns
    Type Description
    Mocker

    AddKnownType<TKnown>(Func<Mocker, Type, TKnown?>?, Func<Mocker, Type, TKnown?>?, Action<Mocker, Type, IFastMock>?, Action<Mocker, TKnown>?, bool, bool)

    Adds a typed custom known-type registration for this Mocker instance.

    Declaration
    public Mocker AddKnownType<TKnown>(Func<Mocker, Type, TKnown?>? directInstanceFactory = null, Func<Mocker, Type, TKnown?>? managedInstanceFactory = null, Action<Mocker, Type, IFastMock>? configureMock = null, Action<Mocker, TKnown>? applyObjectDefaults = null, bool includeDerivedTypes = false, bool replace = false)
    Parameters
    Type Name Description
    Func<Mocker, Type, TKnown> directInstanceFactory
    Func<Mocker, Type, TKnown> managedInstanceFactory
    Action<Mocker, Type, IFastMock> configureMock
    Action<Mocker, TKnown> applyObjectDefaults
    bool includeDerivedTypes
    bool replace
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    TKnown

    AddProperties(Type, object?, params KeyValuePair<string, object>[])

    Populates readable properties on an object using either explicit values or resolved dependencies.

    Declaration
    public object? AddProperties(Type type, object? obj, params KeyValuePair<string, object>[] data)
    Parameters
    Type Name Description
    Type type
    object obj
    KeyValuePair<string, object>[] data
    Returns
    Type Description
    object

    AddProperties<T>(T, params KeyValuePair<string, object>[])

    Populates readable properties on an object using either explicit values or resolved dependencies.

    Declaration
    public T? AddProperties<T>(T obj, params KeyValuePair<string, object>[] data)
    Parameters
    Type Name Description
    T obj
    KeyValuePair<string, object>[] data
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    AddType(Type, Type, Func<Mocker, object>?, bool, params object?[]?)

    Registers an interface-to-concrete mapping for object resolution.

    Declaration
    public Mocker AddType(Type tInterface, Type tClass, Func<Mocker, object>? createFunc = null, bool replace = false, params object?[]? args)
    Parameters
    Type Name Description
    Type tInterface

    The service or abstraction type to resolve.

    Type tClass

    The concrete implementation type to construct.

    Func<Mocker, object> createFunc

    Optional factory used instead of the default constructor path.

    bool replace

    True to replace an existing registration for the same service type.

    object[] args

    Optional constructor or factory arguments associated with the registration.

    Returns
    Type Description
    Mocker

    The current Mocker instance.

    AddType<T>(Func<Mocker, T>?, bool, params object?[]?)

    Registers a concrete type using an optional typed factory.

    Declaration
    public Mocker AddType<T>(Func<Mocker, T>? createFunc = null, bool replace = false, params object?[]? args) where T : class
    Parameters
    Type Name Description
    Func<Mocker, T> createFunc
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    T

    AddType<T>(T, bool)

    Registers a concrete instance so future object resolution returns that instance instead of auto-creating a mock.

    Declaration
    public Mocker AddType<T>(T value, bool replace = false)
    Parameters
    Type Name Description
    T value
    bool replace
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    T
    Examples

    Use this overload when a dependency should be a known runtime object rather than a tracked mock.

    var fileSystem = new MockFileSystem();
    

    var mocker = new Mocker() .AddType<IFileSystem>(fileSystem);

    var resolvedFileSystem = mocker.GetRequiredObject<IFileSystem>(); resolvedFileSystem.Should().BeSameAs(fileSystem);

    AddType<TInterface, TClass>(bool, params object?[]?)

    Registers a type as its own implementation for future resolution.

    Declaration
    public Mocker AddType<TInterface, TClass>(bool replace = false, params object?[]? args) where TInterface : class where TClass : class
    Parameters
    Type Name Description
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    TInterface
    TClass

    AddType<TInterface, TClass>(Func<Mocker, TClass>?, bool, params object?[]?)

    Registers a type mapping that uses a typed factory during resolution.

    Declaration
    public Mocker AddType<TInterface, TClass>(Func<Mocker, TClass>? createFunc, bool replace = false, params object?[]? args) where TInterface : class where TClass : class
    Parameters
    Type Name Description
    Func<Mocker, TClass> createFunc
    bool replace
    object[] args
    Returns
    Type Description
    Mocker
    Type Parameters
    Name Description
    TInterface
    TClass

    BuildExpression<T>()

    Builds a wildcard predicate expression using the active mocking provider.

    Declaration
    public static Expression<Func<T, bool>> BuildExpression<T>()
    Returns
    Type Description
    Expression<Func<T, bool>>
    Type Parameters
    Name Description
    T

    CallMethod(InvocationOptions?, Action, params object?[])

    Invokes an action using the supplied invocation options.

    Declaration
    public void CallMethod(InvocationOptions? options, Action action, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    Action action
    object[] args

    CallMethod(InvocationOptions?, Delegate, params object?[])

    Invokes a delegate without using its return value and with explicit invocation options.

    Declaration
    public void CallMethod(InvocationOptions? options, Delegate del, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    Delegate del
    object[] args

    CallMethod(Action, params object?[])

    Invokes an action using the mocker's current invocation defaults.

    Declaration
    public void CallMethod(Action action, params object?[] args)
    Parameters
    Type Name Description
    Action action
    object[] args

    CallMethod(Delegate, params object?[])

    Invokes a delegate without using its return value.

    Declaration
    public void CallMethod(Delegate del, params object?[] args)
    Parameters
    Type Name Description
    Delegate del
    object[] args

    CallMethodAsync<TReturn>(InvocationOptions?, Delegate, params object?[])

    Invokes a delegate and awaits a task result using the supplied invocation options.

    Declaration
    public Task<TReturn> CallMethodAsync<TReturn>(InvocationOptions? options, Delegate del, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    Delegate del
    object[] args
    Returns
    Type Description
    Task<TReturn>
    Type Parameters
    Name Description
    TReturn

    CallMethodAsync<TReturn>(Delegate, params object?[])

    Invokes a delegate and awaits a task result using the mocker's current invocation defaults.

    Declaration
    public Task<TReturn> CallMethodAsync<TReturn>(Delegate del, params object?[] args)
    Parameters
    Type Name Description
    Delegate del
    object[] args
    Returns
    Type Description
    Task<TReturn>
    Type Parameters
    Name Description
    TReturn

    CallMethod<TReturn>(InvocationOptions?, Delegate, params object?[])

    Invokes a delegate and returns its result using the supplied invocation options.

    Declaration
    public TReturn CallMethod<TReturn>(InvocationOptions? options, Delegate del, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    Delegate del
    object[] args
    Returns
    Type Description
    TReturn
    Type Parameters
    Name Description
    TReturn

    CallMethod<TReturn>(InvocationOptions?, Func<TReturn>, params object?[])

    Invokes a function and returns its result using the supplied invocation options.

    Declaration
    public TReturn CallMethod<TReturn>(InvocationOptions? options, Func<TReturn> func, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    Func<TReturn> func
    object[] args
    Returns
    Type Description
    TReturn
    Type Parameters
    Name Description
    TReturn

    CallMethod<TReturn>(Delegate, params object?[])

    Invokes a delegate and returns its result using the mocker's current invocation defaults.

    Declaration
    public TReturn CallMethod<TReturn>(Delegate del, params object?[] args)
    Parameters
    Type Name Description
    Delegate del
    object[] args
    Returns
    Type Description
    TReturn
    Type Parameters
    Name Description
    TReturn

    CallMethod<TReturn>(Func<TReturn>, params object?[])

    Invokes a function and returns its result using the mocker's current invocation defaults.

    Declaration
    public TReturn CallMethod<TReturn>(Func<TReturn> func, params object?[] args)
    Parameters
    Type Name Description
    Func<TReturn> func
    object[] args
    Returns
    Type Description
    TReturn
    Type Parameters
    Name Description
    TReturn

    CreateFastMock<T>(MockCreationOptions?)

    Creates a provider-first mock and registers it.

    Declaration
    public IFastMock<T> CreateFastMock<T>(MockCreationOptions? options = null) where T : class
    Parameters
    Type Name Description
    MockCreationOptions options
    Returns
    Type Description
    IFastMock<T>
    Type Parameters
    Name Description
    T

    CreateInstanceByType<T>(InstanceCreationFlags, params Type?[])

    Creates an instance of T by selecting a constructor that matches the supplied parameter types and creation flags.

    Declaration
    public T? CreateInstanceByType<T>(InstanceCreationFlags flags, params Type?[] parameterTypes) where T : class
    Parameters
    Type Name Description
    InstanceCreationFlags flags
    Type[] parameterTypes
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    CreateInstanceByType<T>(params Type?[])

    Creates an instance of T by selecting a constructor that matches the supplied parameter types.

    Declaration
    public T? CreateInstanceByType<T>(params Type?[] parameterTypes) where T : class
    Parameters
    Type Name Description
    Type[] parameterTypes
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    CreateInstance<T>(InstanceCreationFlags, params object?[])

    Creates an instance of T using the supplied per-call construction flags.

    Declaration
    public T? CreateInstance<T>(InstanceCreationFlags flags, params object?[] args) where T : class
    Parameters
    Type Name Description
    InstanceCreationFlags flags
    object[] args
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    CreateInstance<T>(params object?[])

    Creates an instance of T using the current component-construction defaults.

    Declaration
    public T? CreateInstance<T>(params object?[] args) where T : class
    Parameters
    Type Name Description
    object[] args
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetArgData<T>()

    Builds constructor argument data for T using the mocker's current optional-parameter policy.

    Declaration
    public object?[] GetArgData<T>() where T : class
    Returns
    Type Description
    object[]
    Type Parameters
    Name Description
    T

    GetArgData<T>(InstanceCreationFlags)

    Builds constructor argument data for T using the supplied creation flags.

    Declaration
    public object?[] GetArgData<T>(InstanceCreationFlags flags) where T : class
    Parameters
    Type Name Description
    InstanceCreationFlags flags
    Returns
    Type Description
    object[]
    Type Parameters
    Name Description
    T

    GetArgData<T>(OptionalParameterResolutionMode)

    Builds constructor argument data for T using the supplied optional-parameter policy.

    Declaration
    public object?[] GetArgData<T>(OptionalParameterResolutionMode optionalParameterResolution) where T : class
    Parameters
    Type Name Description
    OptionalParameterResolutionMode optionalParameterResolution
    Returns
    Type Description
    object[]
    Type Parameters
    Name Description
    T

    GetFileSystem()

    Returns the shared mock file system.

    Declaration
    public IFileSystem GetFileSystem()
    Returns
    Type Description
    IFileSystem

    GetFileSystem(Action<MockFileSystem>?)

    Returns the shared mock file system after optionally applying additional configuration.

    Declaration
    public IFileSystem GetFileSystem(Action<MockFileSystem>? configure = null)
    Parameters
    Type Name Description
    Action<MockFileSystem> configure
    Returns
    Type Description
    IFileSystem

    GetKeyedObject(Type, object, Action<object?>?)

    Resolves a keyed object for the specified service type.

    Declaration
    public object? GetKeyedObject(Type type, object serviceKey, Action<object?>? initAction = null)
    Parameters
    Type Name Description
    Type type
    object serviceKey
    Action<object> initAction
    Returns
    Type Description
    object

    GetKeyedObject<T>(object)

    Resolves a keyed object using the supplied service key.

    Declaration
    public T? GetKeyedObject<T>(object serviceKey) where T : class
    Parameters
    Type Name Description
    object serviceKey
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetKeyedObject<T>(object, Action<T?>)

    Resolves a keyed object and invokes an initialization callback with the typed result.

    Declaration
    public T? GetKeyedObject<T>(object serviceKey, Action<T?> init) where T : class
    Parameters
    Type Name Description
    object serviceKey
    Action<T> init
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetList<T>(int, Func<int, T>)

    Creates a list by invoking the supplied indexed factory a fixed number of times.

    Declaration
    public static List<T> GetList<T>(int count, Func<int, T> factory)
    Parameters
    Type Name Description
    int count
    Func<int, T> factory
    Returns
    Type Description
    List<T>
    Type Parameters
    Name Description
    T

    GetList<T>(int, Func<int, T>, Action<int, T>)

    Creates a list by invoking the supplied indexed factory and initializer a fixed number of times.

    Declaration
    public static List<T> GetList<T>(int count, Func<int, T> factory, Action<int, T> init)
    Parameters
    Type Name Description
    int count
    Func<int, T> factory
    Action<int, T> init
    Returns
    Type Description
    List<T>
    Type Parameters
    Name Description
    T

    GetList<T>(int, Func<T>)

    Creates a list by invoking the supplied factory a fixed number of times.

    Declaration
    public static List<T> GetList<T>(int count, Func<T> factory)
    Parameters
    Type Name Description
    int count
    Func<T> factory
    Returns
    Type Description
    List<T>
    Type Parameters
    Name Description
    T

    GetMethodArgData(MethodBase?)

    Builds invocation arguments for a method using the mocker's current optional-parameter policy.

    Declaration
    public object?[] GetMethodArgData(MethodBase? method)
    Parameters
    Type Name Description
    MethodBase method
    Returns
    Type Description
    object[]

    GetMethodArgData(MethodBase?, InvocationOptions?)

    Builds invocation arguments for a method using the supplied invocation options.

    Declaration
    public object?[] GetMethodArgData(MethodBase? method, InvocationOptions? options)
    Parameters
    Type Name Description
    MethodBase method
    InvocationOptions options
    Returns
    Type Description
    object[]

    GetMethodDefaultData(MethodBase?)

    Builds invocation arguments that contain only default values for the supplied method.

    Declaration
    public object?[] GetMethodDefaultData(MethodBase? method)
    Parameters
    Type Name Description
    MethodBase method
    Returns
    Type Description
    object[]

    GetMockModel<T>()

    Gets the tracked mock model for the supplied type parameter.

    Declaration
    public MockModel<T> GetMockModel<T>() where T : class
    Returns
    Type Description
    MockModel<T>
    Type Parameters
    Name Description
    T

    GetNativeMock(Type)

    Gets the provider-native mock object for the supplied runtime type.

    Declaration
    public object GetNativeMock(Type type)
    Parameters
    Type Name Description
    Type type
    Returns
    Type Description
    object

    GetNativeMock<T>(params object?[])

    Gets the provider-native mock object for the supplied type parameter.

    Declaration
    public object GetNativeMock<T>(params object?[] args) where T : class
    Parameters
    Type Name Description
    object[] args
    Returns
    Type Description
    object
    Type Parameters
    Name Description
    T

    GetObject(ParameterInfo)

    Resolves a value for the supplied parameter using the mocker's constructor and object-resolution rules.

    Declaration
    public object? GetObject(ParameterInfo info)
    Parameters
    Type Name Description
    ParameterInfo info

    The parameter to resolve.

    Returns
    Type Description
    object

    The resolved value, or the parameter type default when resolution fails.

    GetObject(Type, Action<object?>?)

    Resolves an object using custom registrations, known types, tracked mocks, and default fallbacks.

    Declaration
    public object? GetObject(Type type, Action<object?>? initAction = null)
    Parameters
    Type Name Description
    Type type

    The requested service or concrete type.

    Action<object> initAction

    Optional callback invoked with the resolved value before it is returned.

    Returns
    Type Description
    object

    The resolved object instance, or the type default when no value can be created.

    GetObject<T>()

    Resolves an object using FastMoq's runtime resolution pipeline.

    Declaration
    public T? GetObject<T>() where T : class
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T
    Examples

    Resolve a dependency after configuring a concrete registration through the provider-first pipeline.

    var fileSystem = new MockFileSystem();
    fileSystem.AddDirectory(@"c:\exports");
    

    var mocker = new Mocker() .AddType<IFileSystem>(fileSystem);

    var resolvedFileSystem = mocker.GetRequiredObject<IFileSystem>();

    resolvedFileSystem.Directory.Exists(@"c:\exports").Should().BeTrue();

    GetObject<T>(Action<T?>)

    Resolves an object and invokes an initialization callback with the typed result.

    Declaration
    public T? GetObject<T>(Action<T?> init) where T : class
    Parameters
    Type Name Description
    Action<T> init
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetObject<T>(object?[])

    Creates an instance of T using the supplied constructor arguments.

    Declaration
    public T? GetObject<T>(object?[] args) where T : class
    Parameters
    Type Name Description
    object[] args
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetOrCreateMock(Type, MockRequestOptions?)

    Gets an existing tracked provider-backed mock or creates and tracks one when it does not yet exist.

    Declaration
    public IFastMock GetOrCreateMock(Type type, MockRequestOptions? options = null)
    Parameters
    Type Name Description
    Type type
    MockRequestOptions options
    Returns
    Type Description
    IFastMock

    GetOrCreateMock<T>(MockRequestOptions?)

    Gets an existing tracked provider-backed mock or creates and tracks one when it does not yet exist.

    Declaration
    public IFastMock<T> GetOrCreateMock<T>(MockRequestOptions? options = null) where T : class
    Parameters
    Type Name Description
    MockRequestOptions options
    Returns
    Type Description
    IFastMock<T>
    Type Parameters
    Name Description
    T
    Examples

    Use GetOrCreateMock<T>(MockRequestOptions?) when the test needs the tracked mock handle itself, not just automatic constructor resolution.

    GetOrCreateMock uses the active FastMoq provider. It does not require the Moq provider to be selected unless the test later calls Moq-specific extensions such as AsMoq(), Setup(...), or Protected().

    var mocker = new Mocker();
    var gateway = mocker.GetOrCreateMock<IOrderGateway>();
    

    var submitter = new OrderSubmitter(gateway.Instance); submitter.Submit(42);

    mocker.Verify<IOrderGateway>(x => x.Publish(42), TimesSpec.Once); mocker.VerifyNoOtherCalls<IOrderGateway>();

    Provider-neutral usage works the same way under the reflection provider because the tracked handle is still an IFastMock<T>.

    using var providerScope = MockingProviderRegistry.Push("reflection");
    

    var mocker = new Mocker(); var dependency = mocker.GetOrCreateMock<IOrderGateway>();

    var submitter = new OrderSubmitter(dependency.Instance); submitter.Submit(42);

    mocker.Verify<IOrderGateway>(x => x.Publish(42), TimesSpec.Once);

    Typical reasons to call it explicitly are: passing Instance into custom component construction, reusing the same tracked mock across calls, resetting it, or using keyed MockRequestOptions. If the test needs Moq-specific setup or verification APIs, select the Moq provider for that test assembly before calling those extensions.

    GetRequiredObject<T>()

    Resolves a required object and throws when no instance can be produced.

    Declaration
    public T GetRequiredObject<T>() where T : class
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    GetStringContent(HttpContent?)

    Reads the string content from an HttpContent instance, returning an empty string when the content is null.

    Declaration
    public Task<string> GetStringContent(HttpContent? content)
    Parameters
    Type Name Description
    HttpContent content
    Returns
    Type Description
    Task<string>

    HasParameterlessConstructor(Type, bool)

    Determines whether the supplied type exposes a parameterless constructor.

    Declaration
    public bool HasParameterlessConstructor(Type type, bool nonPublic = false)
    Parameters
    Type Name Description
    Type type
    bool nonPublic
    Returns
    Type Description
    bool

    InvokeMethod(InvocationOptions?, object?, string, bool, params object?[])

    Invokes a method on the supplied instance using the supplied invocation options.

    Declaration
    public object? InvokeMethod(InvocationOptions? options, object? instance, string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    object instance
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object

    InvokeMethod(object?, string, bool, params object?[])

    Invokes a method on the supplied instance using the mocker's current invocation defaults.

    Declaration
    public object? InvokeMethod(object? instance, string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    object instance
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object

    InvokeMethod<T>(InvocationOptions?, object?, string, bool, params object?[])

    Invokes a method declared on T using the supplied invocation options.

    Declaration
    public object? InvokeMethod<T>(InvocationOptions? options, object? instance, string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    object instance
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object
    Type Parameters
    Name Description
    T

    InvokeMethod<T>(InvocationOptions?, string, bool, params object?[])

    Invokes a method declared on T without an explicit instance parameter using the supplied invocation options.

    Declaration
    public object? InvokeMethod<T>(InvocationOptions? options, string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    InvocationOptions options
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object
    Type Parameters
    Name Description
    T

    InvokeMethod<T>(object?, string, bool, params object?[])

    Invokes a method declared on T using the mocker's current invocation defaults.

    Declaration
    public object? InvokeMethod<T>(object? instance, string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    object instance
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object
    Type Parameters
    Name Description
    T

    InvokeMethod<T>(string, bool, params object?[])

    Invokes a method declared on T without an explicit instance parameter.

    Declaration
    public object? InvokeMethod<T>(string methodName, bool nonPublic = false, params object?[] args)
    Parameters
    Type Name Description
    string methodName
    bool nonPublic
    object[] args
    Returns
    Type Description
    object
    Type Parameters
    Name Description
    T

    Scenario<T>(T?)

    Creates a new fluent scenario builder for the specified component type using provider-first creation.

    Declaration
    public ScenarioBuilder<T> Scenario<T>(T? instance = null) where T : class
    Parameters
    Type Name Description
    T instance
    Returns
    Type Description
    ScenarioBuilder<T>
    Type Parameters
    Name Description
    T

    UseLenientPreset()

    Applies the predefined lenient behavior preset. This is broader than disabling FailOnUnconfigured alone.

    Declaration
    public void UseLenientPreset()

    UseStrictPreset()

    Applies the predefined strict behavior preset. This is broader than FastMoq.Mocker.Strict, which is retained for backward compatibility.

    Declaration
    public void UseStrictPreset()

    VerifyNoOtherCalls<T>()

    Ensures no other calls were made for a given mock (provider-first only).

    Declaration
    public void VerifyNoOtherCalls<T>() where T : class
    Type Parameters
    Name Description
    T

    Verify<T>(Expression<Action<T>>, TimesSpec?)

    Provider-first verification helper (provider agnostic).

    Declaration
    public void Verify<T>(Expression<Action<T>> expression, TimesSpec? times = null) where T : class
    Parameters
    Type Name Description
    Expression<Action<T>> expression
    TimesSpec? times
    Type Parameters
    Name Description
    T

    Extension Methods

    DbContextMockerExtensions.GetDbContextHandle<TContext>(Mocker, DbContextHandleOptions<TContext>?)
    DbContextMockerExtensions.GetMockDbContext(Mocker, Type)
    DbContextMockerExtensions.GetMockDbContext<TContext>(Mocker)
    MockerBooleanExtensions.Contains(Mocker, Type)
    MockerBooleanExtensions.Contains<T>(Mocker)
    MockerCreationExtensions.CreateInstance<T, TParam1>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1>(Mocker, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2>(Mocker, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3>(Mocker, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3, TParam4>(Mocker, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(Mocker, InstanceCreationFlags, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateInstance<T, TParam1, TParam2, TParam3, TParam4, TParam5>(Mocker, Dictionary<Type, object?>)
    MockerCreationExtensions.CreateMockInternal(Mocker, Type, IReadOnlyCollection<object?>?, bool, bool)
    MockerCreationExtensions.CreateMockInternal<T>(Mocker, bool)
    MockerHttpExtensions.CreateHttpClient(Mocker, string, string, HttpStatusCode, string)
    MockerHttpExtensions.WhenHttpRequest(Mocker, Func<HttpRequestMessage, bool>, Func<HttpResponseMessage>)
    MockerHttpExtensions.WhenHttpRequest(Mocker, HttpMethod, string, Func<HttpResponseMessage>)
    MockerHttpExtensions.WhenHttpRequestJson(Mocker, HttpMethod, string, string, HttpStatusCode)
    ObjectExtensions.RaiseIfNull<T>(T?, string?, string?, int?, string?)
    TestClassExtensions.GetAmbiguousConstructorImplementationException(Mocker, Type)
    TestClassExtensions.GetAmbiguousImplementationException(Mocker, string)
    TestClassExtensions.GetAmbiguousImplementationException(Mocker, Type, ICollection<Type>?)
    TestClassExtensions.GetFieldValue<TObject>(TObject, string, TObject?)
    TestClassExtensions.GetField<TObject>(TObject, string)
    TestClassExtensions.GetMemberName<T, TValue>(T, Expression<Func<T, TValue>>)
    TestClassExtensions.GetMember<T, TValue>(T, Expression<Func<T, TValue>>)
    TestClassExtensions.GetMethodValue<TObject>(TObject, string, object?, params object[])
    TestClassExtensions.GetMethod<TObject>(TObject, string)
    TestClassExtensions.GetPropertyValue<TObject>(TObject, string, object?)
    TestClassExtensions.GetProperty<TObject>(TObject, string)
    TestClassExtensions.GetTypeFromInterface(Mocker, Type, List<Type>?)
    TestClassExtensions.SetFieldValue<TObject>(TObject, string, object?)
    TestClassExtensions.SetPropertyValue<TObject>(TObject, string, object?)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string, TimesSpec)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string, Exception?, int?)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string, Exception?, int?, int)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string, Exception?, int?, TimesSpec?)
    TestClassExtensions.VerifyLogged(Mocker, LogLevel, string, int)
    MockerHttpMoqExtensions.SetupHttpMessage(object, Func<HttpResponseMessage>, Expression?, Expression?)
    MockerHttpMoqExtensions.SetupMessageAsync<TMock, TReturn>(object, Expression<Func<TMock, Task<TReturn>>>, Func<TReturn>)
    MockerHttpMoqExtensions.SetupMessageProtectedAsync<TMock, TReturn>(object, string, Func<TReturn>, params object?[]?)
    MockerHttpMoqExtensions.SetupMessageProtected<TMock, TReturn>(object, string, Func<TReturn>, params object?[]?)
    MockerHttpMoqExtensions.SetupMessage<TMock, TReturn>(object, Expression<Func<TMock, TReturn>>, Func<TReturn>)
    TestClassExtensions.GetFieldInfo<TType>(object, string)
    TestClassExtensions.GetFieldValue<T>(object?, FieldInfo)
    TestClassExtensions.GetFieldValue<T, TType>(object, string)
    In this article
    Back to top
    Generated 2026-04-08 00:16 UTC