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. |
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 |
| 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)
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 |