FastMoq
Search Results for

    Show / Hide Table of Contents

    Class DbContextMock<TEntity>

    Wrapper for Mock. Implements the Moq.Mock<T>

    Inheritance
    object
    Mock
    Mock<TEntity>
    DbContextMock<TEntity>
    Implements
    IMock<TEntity>
    IDbContextMock<TEntity>
    IDbContextMock
    Inherited Members
    Mock<TEntity>.ToString()
    Mock<TEntity>.OnGetObject()
    Mock<TEntity>.As<TInterface>()
    Mock<TEntity>.Setup(Expression<Action<TEntity>>)
    Mock<TEntity>.Setup<TResult>(Expression<Func<TEntity, TResult>>)
    Mock<TEntity>.SetupGet<TProperty>(Expression<Func<TEntity, TProperty>>)
    Mock<TEntity>.SetupSet<TProperty>(Action<TEntity>)
    Mock<TEntity>.SetupSet(Action<TEntity>)
    Mock<TEntity>.SetupAdd(Action<TEntity>)
    Mock<TEntity>.SetupRemove(Action<TEntity>)
    Mock<TEntity>.SetupProperty<TProperty>(Expression<Func<TEntity, TProperty>>)
    Mock<TEntity>.SetupProperty<TProperty>(Expression<Func<TEntity, TProperty>>, TProperty)
    Mock<TEntity>.SetupAllProperties()
    Mock<TEntity>.SetupSequence<TResult>(Expression<Func<TEntity, TResult>>)
    Mock<TEntity>.SetupSequence(Expression<Action<TEntity>>)
    Mock<TEntity>.When(Func<bool>)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>, Times)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>, Func<Times>)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>, string)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>, Times, string)
    Mock<TEntity>.Verify(Expression<Action<TEntity>>, Func<Times>, string)
    Mock<TEntity>.Verify<TResult>(Expression<Func<TEntity, TResult>>)
    Mock<TEntity>.Verify<TResult>(Expression<Func<TEntity, TResult>>, Times)
    Mock<TEntity>.Verify<TResult>(Expression<Func<TEntity, TResult>>, Func<Times>)
    Mock<TEntity>.Verify<TResult>(Expression<Func<TEntity, TResult>>, string)
    Mock<TEntity>.Verify<TResult>(Expression<Func<TEntity, TResult>>, Times, string)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>, Times)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>, Func<Times>)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>, string)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>, Times, string)
    Mock<TEntity>.VerifyGet<TProperty>(Expression<Func<TEntity, TProperty>>, Func<Times>, string)
    Mock<TEntity>.VerifySet(Action<TEntity>)
    Mock<TEntity>.VerifySet(Action<TEntity>, Times)
    Mock<TEntity>.VerifySet(Action<TEntity>, Func<Times>)
    Mock<TEntity>.VerifySet(Action<TEntity>, string)
    Mock<TEntity>.VerifySet(Action<TEntity>, Times, string)
    Mock<TEntity>.VerifySet(Action<TEntity>, Func<Times>, string)
    Mock<TEntity>.VerifyAdd(Action<TEntity>)
    Mock<TEntity>.VerifyAdd(Action<TEntity>, Times)
    Mock<TEntity>.VerifyAdd(Action<TEntity>, Func<Times>)
    Mock<TEntity>.VerifyAdd(Action<TEntity>, string)
    Mock<TEntity>.VerifyAdd(Action<TEntity>, Times, string)
    Mock<TEntity>.VerifyAdd(Action<TEntity>, Func<Times>, string)
    Mock<TEntity>.VerifyRemove(Action<TEntity>)
    Mock<TEntity>.VerifyRemove(Action<TEntity>, Times)
    Mock<TEntity>.VerifyRemove(Action<TEntity>, Func<Times>)
    Mock<TEntity>.VerifyRemove(Action<TEntity>, string)
    Mock<TEntity>.VerifyRemove(Action<TEntity>, Times, string)
    Mock<TEntity>.VerifyRemove(Action<TEntity>, Func<Times>, string)
    Mock<TEntity>.VerifyNoOtherCalls()
    Mock<TEntity>.Raise(Action<TEntity>, EventArgs)
    Mock<TEntity>.Raise(Action<TEntity>, params object[])
    Mock<TEntity>.Behavior
    Mock<TEntity>.DefaultValueProvider
    Mock<TEntity>.Object
    Mock<TEntity>.Name
    Mock<TEntity>.Switches
    Mock.Of<T>()
    Mock.Of<T>(MockBehavior)
    Mock.Of<T>(Expression<Func<T, bool>>)
    Mock.Of<T>(Expression<Func<T, bool>>, MockBehavior)
    Mock.Get<T>(T)
    Mock.Verify(params Mock[])
    Mock.VerifyAll(params Mock[])
    Mock.Verify()
    Mock.VerifyAll()
    Mock.SetReturnsDefault<TReturn>(TReturn)
    Mock.DefaultValue
    Mock.Invocations
    Mock.Setups
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: FastMoq.Models
    Assembly: FastMoq.Database.dll
    Syntax
    public class DbContextMock<TEntity> : Mock<TEntity>, IMock<TEntity>, IDbContextMock<TEntity>, IDbContextMock where TEntity : DbContext
    Type Parameters
    Name Description
    TEntity

    The type of the t entity.

    Remarks

    Any interface type can be used for mocking, but for classes, only abstract and virtual members can be mocked.

    The behavior of the mock with regards to the setups and the actual calls is determined by the optional Moq.MockBehavior that can be passed to the Moq.Mock<T>.Mock(Moq.MockBehavior) constructor.

    Examples

    The following example shows establishing setups with specific values for method invocations:

    // Arrange
    var order = new Order(TALISKER, 50);
    var warehouse = new Mock<IWarehouse>();
    warehouse.Setup(w => w.HasInventory(TALISKER, 50)).Returns(true);
    
    // Act
    order.Fill(warehouse.Object);
    
    // Assert
    Assert.True(order.IsFilled);

    The following example shows how to use the Moq.It class to specify conditions for arguments instead of specific values:

    // Arrange
    var order = new Order(TALISKER, 50);
    var warehouse = new Mock<IWarehouse>();
    
    // shows how to expect a value within a range:
    warehouse.Setup(x => x.HasInventory(
                             It.IsAny<string>(),
                             It.IsInRange(0, 100, Range.Inclusive)))
             .Returns(false);
    
    // shows how to throw for unexpected calls.
    warehouse.Setup(x => x.Remove(
                             It.IsAny<string>(),
                             It.IsAny<int>()))
             .Throws(new InvalidOperationException());
    
    // Act
    order.Fill(warehouse.Object);
    
    // Assert
    Assert.False(order.IsFilled);

    Constructors

    DbContextMock()

    Initializes an instance of the mock with Moq.MockBehavior.Default behavior.

    Declaration
    public DbContextMock()
    Examples
    var mock = new Mock<IFormatProvider>();

    DbContextMock(MockBehavior)

    Initializes an instance of the mock with the specified Moq.MockBehavior behavior.

    Declaration
    public DbContextMock(MockBehavior behavior)
    Parameters
    Type Name Description
    MockBehavior behavior

    Behavior of the mock.

    Examples
    var mock = new Mock<IFormatProvider>(MockBehavior.Strict);

    DbContextMock(MockBehavior, params object[])

    Initializes an instance of the mock with a specific Moq.MockBehavior behavior and with the given constructor arguments for the class.

    Declaration
    public DbContextMock(MockBehavior behavior, params object[] args)
    Parameters
    Type Name Description
    MockBehavior behavior

    Behavior of the mock.

    object[] args

    Optional constructor arguments if the mocked type is a class.

    Remarks

    The mock will try to find the best match constructor given the constructor arguments, and invoke that to initialize the instance. This applies only to classes, not interfaces.

    DbContextMock(Expression<Func<TEntity>>, MockBehavior)

    Initializes an instance of the mock using the given constructor call including its argument values and with a specific Moq.MockBehavior behavior.

    Declaration
    public DbContextMock(Expression<Func<TEntity>> newExpression, MockBehavior behavior = MockBehavior.Default)
    Parameters
    Type Name Description
    Expression<Func<TEntity>> newExpression

    Lambda expression that creates an instance of T.

    MockBehavior behavior

    Behavior of the mock.

    Examples
    var mock = new Mock<MyProvider>(() => new MyProvider(someArgument, 25), MockBehavior.Loose);

    DbContextMock(params object[])

    Initializes an instance of the mock with Moq.MockBehavior.Default behavior and with the given constructor arguments for the class. (Only valid when T is a class.)

    Declaration
    public DbContextMock(params object[] args)
    Parameters
    Type Name Description
    object[] args

    Optional constructor arguments if the mocked type is a class.

    Remarks

    The mock will try to find the best match constructor given the constructor arguments, and invoke that to initialize the instance.This applies only for classes, not interfaces.

    Examples
    var mock = new Mock<MyProvider>(someArgument, 25);

    Properties

    CallBase

    Whether the base member virtual implementation will be called for mocked classes if no setup is matched. Defaults to false.

    Declaration
    public override bool CallBase { get; set; }
    Property Value
    Type Description
    bool
    Overrides
    Moq.Mock<TEntity>.CallBase

    Methods

    SetupDbContextSetMethods(PropertyInfo)

    Declaration
    public void SetupDbContextSetMethods(PropertyInfo propertyInfo)
    Parameters
    Type Name Description
    PropertyInfo propertyInfo

    SetupDbSetProperties(PropertyInfo, object)

    Declaration
    public virtual void SetupDbSetProperties(PropertyInfo propertyInfo, object value)
    Parameters
    Type Name Description
    PropertyInfo propertyInfo
    object value

    SetupDbSetPropertyGet(PropertyInfo, object)

    Declaration
    public void SetupDbSetPropertyGet(PropertyInfo propertyInfo, object value)
    Parameters
    Type Name Description
    PropertyInfo propertyInfo
    object value

    SetupDbSets(Mocker)

    Declaration
    public DbContextMock<TEntity> SetupDbSets(Mocker mocks)
    Parameters
    Type Name Description
    Mocker mocks
    Returns
    Type Description
    DbContextMock<TEntity>

    SetupSetMethod(Type, Delegate, Type[]?, object?[]?)

    Declaration
    public void SetupSetMethod(Type setType, Delegate propValueDelegate, Type[]? types = null, object?[]? parameters = null)
    Parameters
    Type Name Description
    Type setType
    Delegate propValueDelegate
    Type[] types
    object[] parameters

    Implements

    Moq.IMock<T>
    IDbContextMock<TEntity>
    IDbContextMock

    Extension Methods

    ObjectExtensions.RaiseIfNull<T>(T?, string?, string?, int?, string?)
    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.SetFieldValue<TObject>(TObject, string, object?)
    TestClassExtensions.SetPropertyValue<TObject>(TObject, string, object?)
    MockerCreationExtensions.SetupMockProperty<TMock>(Mock<TMock>, Expression<Func<TMock, object>>, object)
    MockerCreationExtensions.SetupMockProperty<TMock>(Mock<TMock>, PropertyInfo, object)
    MockerCreationExtensions.SetupMockProperty<TMock>(Mock<TMock>, string, object)
    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)

    See Also

    Mock<T>
    In this article
    Back to top
    Generated 2026-04-08 12:38 UTC