Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql 1.1.0

Prefix Reserved
dotnet add package Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql --version 1.1.0
                    
NuGet\Install-Package Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql -Version 1.1.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="Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql --version 1.1.0
                    
#r "nuget: Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql, 1.1.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.
#:package Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Microsoft.Learn.AzureFunctionsTesting.Extension.MockSql&version=1.1.0
                    
Install as a Cake Tool

Azure Functions Integration Testing Framework - Mocking SqlServer

If your Azure Functions app uses SqlServer, you can use this package to make mocking easy with no code changes required to your function app code.

Prerequisites

Setup

In your TestStartup, call builder.UseSqlServer(). If your code connects to multiple SqlServers, you can use the overload that takes a name and call .UseSqlServer() multiple times, once for each database you use.

You can use either .sql files or DacPac files to seed your database. .UseSqlServer() returns an object with the connection string of the local DB, which you can use to configure your function app however you would normally pass in your connection string:

var sqlConnectionString = builder.UseSqlServer().WithSqlScript(@"Helpers\setup.sql").ConnectionString;

builder.ConfigureEnvironmentVariables(env =>
{
	env.Add("SqlConnectionString", sqlInfo.ConnectionString);
});

Usage

In your tests, you can call fixture.GetSqlDatabase() (optionally passing in a name if you configured multiple DBs) which will return a LocalDatabase object which contains the connection string of the local DB.

You can use this connection string to create a SqlConnection object and execute your SQL commands as needed.

A convenient approach is to wrap your logic in an extension method like this:

public static async Task AddUserToDatabase<T>(this FunctionFixture<T> fixture, string userId, string name) where T : IFunctionTestStartup
{
    var db = fixture.GetSqlDatabase();
    if (db == null) return;

    var parameters = new List<SqlParameter>
    {
        new SqlParameter("@userId", userId),
        new SqlParameter("@name", name)
    }.ToArray();

    using var connection = new SqlConnection(db.ConnectionString);
    connection.Open();
    using var command = connection.CreateCommand();
    command.CommandType = System.Data.CommandType.StoredProcedure;
    command.CommandText = "CreateUser";
    command.Parameters.AddRange(parameters);
    await command.ExecuteNonQueryAsync();
}

Then in your tests, you can do stuff like:

await fixture.AddUserToDatabase("123", "Test User");

Which makes per-test data setup straightforward.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 405 9/24/2024
1.0.0 226 8/15/2024