TestableDbContext.Mock 1.0.0

dotnet add package TestableDbContext.Mock --version 1.0.0
NuGet\Install-Package TestableDbContext.Mock -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TestableDbContext.Mock" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestableDbContext.Mock --version 1.0.0
#r "nuget: TestableDbContext.Mock, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TestableDbContext.Mock as a Cake Addin
#addin nuget:?package=TestableDbContext.Mock&version=1.0.0

// Install TestableDbContext.Mock as a Cake Tool
#tool nuget:?package=TestableDbContext.Mock&version=1.0.0

TestableDbContext.Mock

Effortless DbContext and DbSet Mocking for Unit Tests

Description

TestableDbContext.Mock is a lightweight and easy-to-use package that simplifies the process of mocking Entity Framework Core DbContexts and DbSets for unit testing. It uses the Moq framework to create mock instances of DbContext and DbSet, allowing you to focus on writing testable code and ensuring the correctness of your data access layer. With its extension methods and fluent API, TestableDbContext.Mock makes it a breeze to set up and configure mock instances for your data context and entity sets.

Installation

To install TestableDbContext.Mock, run the following command in your Package Manager Console:

Install-Package TestableDbContext.Mock

Alternatively, you can search for TestableDbContext.Mock in the NuGet Package Manager.

Usage

Mocking DbContext

To create a mock instance of your DbContext, simply call the GetMockContext method:

var mockContext = MockDbContext<MyDbContext>.GetMockContext();

You can also specify the MockBehavior as an optional parameter:

var mockContext = MockDbContext<MyDbContext>.GetMockContext(MockBehavior.Strict);

Mocking DbSet

To create a mock instance of a DbSet, first create an IEnumerable of your entity type containing the data you want to use for testing, and then call the GetMockDbSet method:

var testData = new List<MyEntity>
{
    new MyEntity { Id = 1, Name = "Entity1" },
    new MyEntity { Id = 2, Name = "Entity2" }
};

var mockDbSet = MockDbContext.GetMockDbSet(testData);

Assigning Mock DbSet to Mock DbContext

After creating the mock DbSet, assign it to the appropriate property on your mock DbContext:

mockContext.Setup(x => x.MyEntities).Returns(mockDbSet.Object);

Example

Here's a complete example of using TestableDbContext.Mock to create a mock DbContext and DbSet for testing:

using Moq;
using Xunit;
using TestableDbContext.Mock;
using Microsoft.EntityFrameworkCore;

public class MyDbContextTests
{
    [Fact]
    public void TestMethod()
    {
        // Arrange
        var testData = new List<MyEntity>
        {
            new MyEntity { Id = 1, Name = "Entity1" },
            new MyEntity { Id = 2, Name = "Entity2" }
        };

        var mockDbSet = MockDbContext.GetMockDbSet(testData);

        var mockContext = MockDbContext<MyDbContext>.GetMockContext();
        mockContext.Setup(x => x.MyEntities).Returns(mockDbSet.Object);

        // Act
        // Perform your test operations using the mockContext.Object

        // Assert
        // Verify the expected behavior
    }
}

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TestableDbContext.Mock:

Package Downloads
ConfiguredSqlConnection

The NuGet package is a collection of utilities for working with SQL Server database connections using environment settings and secure connection strings.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 2,462 5/5/2023