FastMoq
Search Results for

    Show / Hide Table of Contents

    Class TestClassExtensions

    Helper methods for reflection-based assertions, constructor-guard verification, and logger verification helpers used in tests.

    Inheritance
    object
    TestClassExtensions
    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 TestClassExtensions
    Examples

    The logger helpers are commonly used to keep tests focused on intent instead of verbose Moq verification expressions.

    Mocks.GetOrCreateMock<ILogger<CheckoutService>>();
    

    Component.Submit(order);

    Mocks.VerifyLogged(LogLevel.Information, "Order submitted", TimesSpec.Once);

    Methods

    AddFiles(MockFileSystem, IDictionary<string, MockFileData>)

    Declaration
    public static void AddFiles(this MockFileSystem fileSystem, IDictionary<string, MockFileData> files)
    Parameters
    Type Name Description
    MockFileSystem fileSystem
    IDictionary<string, MockFileData> files

    CallGenericMethod(Type, object, string?, Type[]?, object[]?)

    Calls the generic method.

    Declaration
    public static object? CallGenericMethod(this Type typeParameter, object obj, string? methodName = null, Type[]? parameterTypes = null, object[]? parameters = null)
    Parameters
    Type Name Description
    Type typeParameter

    The type parameter.

    object obj

    The object.

    string methodName

    Name of the method.

    Type[] parameterTypes

    The parameter types.

    object[] parameters

    The parameters.

    Returns
    Type Description
    object

    Calls the generic method.

    Exceptions
    Type Condition
    ArgumentNullException
    MissingMethodException

    EnsureNullCheckThrown(Action, string, string?, Action<string>?)

    Ensures the null check thrown.

    Declaration
    public static void EnsureNullCheckThrown(this Action action, string parameterName, string? constructorName = "", Action<string>? output = null)
    Parameters
    Type Name Description
    Action action

    The action.

    string parameterName

    Name of the parameter.

    string constructorName

    Name of the constructor.

    Action<string> output

    The output.

    Exceptions
    Type Condition
    ArgumentNullException

    EnsureNullCheckThrown(Action, string, string?, ITestOutputHelper?)

    Ensures the null check thrown.

    Declaration
    public static void EnsureNullCheckThrown(this Action action, string parameterName, string? constructorName, ITestOutputHelper? output)
    Parameters
    Type Name Description
    Action action

    The action.

    string parameterName

    Name of the parameter.

    string constructorName

    Name of the constructor.

    ITestOutputHelper output

    The output.

    GetAmbiguousConstructorImplementationException(Mocker, Type)

    Throws the ambiguous constructor implementation exception.

    Declaration
    public static AmbiguousImplementationException GetAmbiguousConstructorImplementationException(this Mocker mocker, Type tType)
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Type tType

    Type of the t.

    Returns
    Type Description
    AmbiguousImplementationException

    System.Runtime.AmbiguousImplementationException.

    GetAmbiguousImplementationException(Mocker, string)

    Throws the ambiguous implementation exception.

    Declaration
    public static AmbiguousImplementationException GetAmbiguousImplementationException(this Mocker mocker, string message)
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    string message

    The message.

    Returns
    Type Description
    AmbiguousImplementationException

    System.Runtime.AmbiguousImplementationException.

    GetAmbiguousImplementationException(Mocker, Type, ICollection<Type>?)

    Gets the ambiguous implementation exception.

    Declaration
    public static AmbiguousImplementationException GetAmbiguousImplementationException(this Mocker mocker, Type tType, ICollection<Type>? types = null)
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Type tType

    Type of the t.

    ICollection<Type> types

    The types.

    Returns
    Type Description
    AmbiguousImplementationException

    System.Runtime.AmbiguousImplementationException.

    GetDefaultValue(Type)

    Gets the default value.

    Declaration
    public static object? GetDefaultValue(this Type type)
    Parameters
    Type Name Description
    Type type

    The type.

    Returns
    Type Description
    object

    Nullable<T>.

    GetFieldInfo<TType>(object, string)

    Gets the field information.

    Declaration
    public static FieldInfo GetFieldInfo<TType>(this object _, string name)
    Parameters
    Type Name Description
    object _

    The object.

    string name

    The name.

    Returns
    Type Description
    FieldInfo

    System.Nullable<FieldInfo>.

    Type Parameters
    Name Description
    TType

    The type of the t type.

    GetFieldValue<T>(object?, FieldInfo)

    Gets the field value.

    Declaration
    public static T? GetFieldValue<T>(this object? obj, FieldInfo field)
    Parameters
    Type Name Description
    object obj

    The object.

    FieldInfo field

    The field.

    Returns
    Type Description
    T

    System.Nullable<T>.

    Type Parameters
    Name Description
    T

    GetFieldValue<TObject>(TObject, string, TObject?)

    Gets the field value.

    Declaration
    public static object? GetFieldValue<TObject>(this TObject obj, string name, TObject? defaultValue = null) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    TObject defaultValue

    The default value.

    Returns
    Type Description
    object

    Nullable<T>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetFieldValue<T, TType>(object, string)

    Gets the field value.

    Declaration
    public static T? GetFieldValue<T, TType>(this object obj, string name)
    Parameters
    Type Name Description
    object obj

    The object.

    string name

    The name.

    Returns
    Type Description
    T

    System.Nullable<T>.

    Type Parameters
    Name Description
    T
    TType

    The type of the t type.

    GetField<TObject>(TObject, string)

    Gets the field.

    Declaration
    public static FieldInfo? GetField<TObject>(this TObject obj, string name) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    Returns
    Type Description
    FieldInfo

    Nullable<T>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetMemberExpression(Expression)

    Gets the member expression.

    Declaration
    public static MemberExpression GetMemberExpression(this Expression method)
    Parameters
    Type Name Description
    Expression method

    The method.

    Returns
    Type Description
    MemberExpression

    MemberExpression.

    GetMemberExpression<T>(Expression<T>)

    Gets the member expression.

    Declaration
    public static MemberExpression GetMemberExpression<T>(this Expression<T> method)
    Parameters
    Type Name Description
    Expression<T> method

    The method.

    Returns
    Type Description
    MemberExpression

    MemberExpression.

    Type Parameters
    Name Description
    T

    GetMemberName(Expression)

    Gets the name of the member.

    Declaration
    public static string GetMemberName(this Expression memberLambda)
    Parameters
    Type Name Description
    Expression memberLambda

    The member lambda.

    Returns
    Type Description
    string

    System.String.

    GetMemberName<T, TValue>(T, Expression<Func<T, TValue>>)

    Gets the name of the member.

    Declaration
    public static string GetMemberName<T, TValue>(this T _, Expression<Func<T, TValue>> memberLambda)
    Parameters
    Type Name Description
    T _

    The .

    Expression<Func<T, TValue>> memberLambda

    The member lambda.

    Returns
    Type Description
    string

    System.String.

    Type Parameters
    Name Description
    T
    TValue

    The type of the t value.

    GetMember<T, TValue>(T, Expression<Func<T, TValue>>)

    Gets the property value based on lambda.

    Declaration
    public static MemberInfo GetMember<T, TValue>(this T _, Expression<Func<T, TValue>> memberLambda)
    Parameters
    Type Name Description
    T _

    The object.

    Expression<Func<T, TValue>> memberLambda

    The member lambda.

    Returns
    Type Description
    MemberInfo

    System.Nullable<TValue>.

    Type Parameters
    Name Description
    T
    TValue

    The type of the t value.

    GetMethodValue<TObject>(TObject, string, object?, params object[])

    Gets the method value.

    Declaration
    public static object? GetMethodValue<TObject>(this TObject obj, string name, object? defaultValue = null, params object[] args) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    object defaultValue

    The default value.

    object[] args

    The arguments.

    Returns
    Type Description
    object

    System.Nullable<System.Object>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetMethod<TObject>(TObject, string)

    Gets the method.

    Declaration
    public static MethodInfo? GetMethod<TObject>(this TObject obj, string name) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    Returns
    Type Description
    MethodInfo

    Nullable<T>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetPropertyValue<TObject>(TObject, string, object?)

    Gets the property value.

    Declaration
    public static object? GetPropertyValue<TObject>(this TObject obj, string name, object? defaultValue = null) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    object defaultValue

    The default value.

    Returns
    Type Description
    object

    Nullable<T>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetProperty<TObject>(TObject, string)

    Gets the property.

    Declaration
    public static PropertyInfo? GetProperty<TObject>(this TObject obj, string name)
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    Returns
    Type Description
    PropertyInfo

    Nullable<T>.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    GetTestData(IReadOnlyList<object>?, int, ParameterInfo)

    Gets the test data.

    Declaration
    public static object? GetTestData(this IReadOnlyList<object>? testData, int i, ParameterInfo p)
    Parameters
    Type Name Description
    IReadOnlyList<object> testData

    The test data.

    int i

    The i.

    ParameterInfo p

    The p.

    Returns
    Type Description
    object

    object of the test data.

    GetTypeFromInterface(Mocker, Type, List<Type>?)

    Gets the type from interface.

    Declaration
    public static Type GetTypeFromInterface(this Mocker mocker, Type tType, List<Type>? typeList = null)
    Parameters
    Type Name Description
    Mocker mocker

    The mocker.

    Type tType

    Type of the t.

    List<Type> typeList

    The type list.

    Returns
    Type Description
    Type

    Type.

    Exceptions
    Type Condition
    AmbiguousImplementationException

    SetFieldValue<TObject>(TObject, string, object?)

    Sets the field value.

    Declaration
    public static void SetFieldValue<TObject>(this TObject obj, string name, object? value) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    object value

    The value.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    SetPropertyValue<TObject>(TObject, string, object?)

    Sets the property value.

    Declaration
    public static void SetPropertyValue<TObject>(this TObject obj, string name, object? value) where TObject : class?
    Parameters
    Type Name Description
    TObject obj

    The object.

    string name

    The name.

    object value

    The value.

    Type Parameters
    Name Description
    TObject

    The type of the t object.

    VerifyLogged(Mocker, LogLevel, string)

    Provider-agnostic logger verification based on captured ILogger callbacks.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    Examples

    Prefer VerifyLogged(Mocker, LogLevel, string) in general documentation because it works through FastMoq's captured log entries instead of a provider-specific mock API.

    Component.Submit(order);
    

    Mocks.VerifyLogged(LogLevel.Information, "Order submitted");

    VerifyLogged(Mocker, LogLevel, string, TimesSpec)

    Provider-agnostic logger verification based on captured ILogger callbacks using a provider-neutral count specification.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message, TimesSpec times)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    TimesSpec times

    VerifyLogged(Mocker, LogLevel, string, Exception?, int?)

    Provider-agnostic logger verification based on captured ILogger callbacks. Defaults to at least one matching log entry when no explicit count specification is supplied.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message, Exception? exception, int? eventId = null)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    Exception exception
    int? eventId

    VerifyLogged(Mocker, LogLevel, string, Exception?, int?, int)

    Provider-agnostic logger verification based on captured ILogger callbacks using an exact match count.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message, Exception? exception, int? eventId, int times)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    Exception exception
    int? eventId
    int times

    VerifyLogged(Mocker, LogLevel, string, Exception?, int?, TimesSpec?)

    Provider-agnostic logger verification based on captured ILogger callbacks.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message, Exception? exception, int? eventId, TimesSpec? times)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    Exception exception
    int? eventId
    TimesSpec? times

    VerifyLogged(Mocker, LogLevel, string, int)

    Provider-agnostic logger verification based on captured ILogger callbacks using an exact match count.

    Declaration
    public static void VerifyLogged(this Mocker mocker, LogLevel logLevel, string message, int times)
    Parameters
    Type Name Description
    Mocker mocker
    LogLevel logLevel
    string message
    int times
    In this article
    Back to top
    Generated 2026-04-08 12:38 UTC