Class DbContextMock<TEntity>
Wrapper for Mock. Implements the Moq.Mock<T>
Inherited Members
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. |
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 |
| 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
Methods
SetupDbContextSetMethods(PropertyInfo)
Configures the Set<TEntity> style methods on the mocked DbContext for the supplied DbSet property.
Declaration
public void SetupDbContextSetMethods(PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The DbSet property being mapped back to the corresponding |
SetupDbSetProperties(PropertyInfo, object)
Configures DbSet-related properties on the mocked DbContext.
Declaration
public virtual void SetupDbSetProperties(PropertyInfo propertyInfo, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The DbSet property to configure. |
| object | value | The value the property should return. |
SetupDbSetPropertyGet(PropertyInfo, object)
Configures the getter for a DbSet property on the mocked DbContext.
Declaration
public void SetupDbSetPropertyGet(PropertyInfo propertyInfo, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The DbSet property whose getter should be configured. |
| object | value | The value the getter should return. |
SetupDbSets(Mocker)
Configures all DbSet properties and related helper methods for the current DbContext mock.
Declaration
public DbContextMock<TEntity> SetupDbSets(Mocker mocks)
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocks | The owning mocker used to resolve nested set dependencies. |
Returns
| Type | Description |
|---|---|
| DbContextMock<TEntity> | The current DbContext mock instance for fluent chaining. |
SetupSetMethod(Type, Delegate, Type[]?, object?[]?)
Configures the generic Set<TEntity> method for the supplied entity type.
Declaration
public void SetupSetMethod(Type setType, Delegate propValueDelegate, Type[]? types = null, object?[]? parameters = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | setType | The entity type represented by the configured DbSet. |
| Delegate | propValueDelegate | A delegate that returns the DbSet instance for the configured entity type. |
| Type[] | types | Optional generic type arguments to apply when building the setup expression. |
| object[] | parameters | Optional method parameters to use while building the setup expression. |