TestBae 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package TestBae --version 0.0.2
                    
NuGet\Install-Package TestBae -Version 0.0.2
                    
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="TestBae" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TestBae" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="TestBae" />
                    
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 TestBae --version 0.0.2
                    
#r "nuget: TestBae, 0.0.2"
                    
#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 TestBae@0.0.2
                    
#: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=TestBae&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=TestBae&version=0.0.2
                    
Install as a Cake Tool

TestBae

A set of base test classes that take care of mocking / dependency injection / automapper

Installation

TestBae is available as a NuGet package. You can add it to your project using one of the following methods:

Using .NET CLI

dotnet add package TestBae

Using Package Manager Console in Visual Studio

Install-Package TestBae

Using PackageReference in your project file

<PackageReference Include="TestBae" Version="0.0.2" />

Using Visual Studio

  1. Right-click on your project in Solution Explorer
  2. Select "Manage NuGet Packages..."
  3. Switch to the "Browse" tab
  4. Search for "TestBae"
  5. Click "Install"

Classes

BaseTest

This is an abstract class used in unit tests class to automatically setup automapper and puts your mocks into dependency injection.

Setup

In your unit test class reference the class you are testing. In this example the PermissionsController is the class being tested.

public class PermissionsControllerTests : BaseTest<PermissionsController>

The BaseTest class will look for automapper profiles in the assembly the PermissionsController comes from.

Next, override the ConfigureServices method.

protected override void ConfigureServices(IServiceCollection services)
        {
            _mockExternalEntityManager = new Mock<IExternalEntityManager>();
            _microsoftGraphUserAdapterMock = new Mock<IMicrosoftGraphUserAdapter>();
            _mockLogger = new Mock<ILogger<PermissionsController>>();

            //these are mocks for the controller
            services.AddTransient(_ => _mockExternalEntityManager.Object);
            services.AddTransient(_ => _microsoftGraphUserAdapterMock.Object);
            services.AddTransient(_ => _mockLogger.Object);

            //this is stuff that AutoMapper cares about
            _httpcontext = new DefaultHttpContext();
            var _httpContextAccessor = new Mock<IHttpContextAccessor>();
            _httpContextAccessor
                .Setup(x => x.HttpContext)
                .Returns(_httpcontext);

            services.AddSingleton(_httpContextAccessor.Object);
            services.AddSingleton<PagingResponseConverter<ExternalEntity, ExternalEntity>>();

            base.ConfigureServices(services);
        }

All the mocks added to the services will be injected into the test class and automapper (if it needs it). The base class will then create an instance of the PermissionsController and make it available in the TestSubject property of the class.

An example test:

[Fact]
public async Task GetGroupsByUser_UserExists_ReturnsOK()
{
    //Arrange
    var groups = new List<string> { "GroupA", "GroupB", "GroupC" };
    var objectId = "0e14e273-4282-4e5f-a368-4b0a9f266be5";

    _microsoftGraphUserAdapterMock.Setup(x => x.GetAssignedGroupsAsync(It.IsAny<string>())).ReturnsAsync(groups);

    //Act
    var response = await TestSubject.GetGroupsByUser(objectId);

    //Assert
    Assert.IsType<ActionResult<GroupAssignmentResponse>>(response);
    Assert.NotNull(response.Value.Groups);
    Assert.True(response.Value.Groups.Count == 3);
}
Properties
  • TestSubject
    • This is the class you are testing setup by the base class and the dependencies that were configured.
  • Mapper
    • An instance of automapper setup by the base class and the dependencies that were configured.
  • ServiceProvider
    • The service provider with all the dependencies.

Local NuGet Package Development

Creating Local NuGet Packages

To create a local NuGet package for testing:

  1. Build the project in Release mode:

    dotnet build -c Release
    
  2. Pack the project to create a NuGet package:

    dotnet pack -c Release -o [LOCAL_PACKAGES_PATH]
    

    Replace [LOCAL_PACKAGES_PATH] with your platform-specific path:

    • Windows: C:\LocalPackages
    • macOS/Linux: ~/LocalPackages

This will create a .nupkg file in your LocalPackages directory.

Setting Up Local NuGet Feed

  1. Create a LocalPackages directory in a location accessible to all your projects:

    Windows:

    mkdir C:\LocalPackages
    

    macOS/Linux:

    mkdir ~/LocalPackages
    
  2. Edit or create a nuget.config file in your .nuget folder (typically ~/.nuget/NuGet/ on macOS/Linux or %APPDATA%\NuGet\ on Windows) with the following content:

    Windows:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        <add key="LocalPackages" value="C:\LocalPackages" />
      </packageSources>
    </configuration>
    

    macOS/Linux:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
        <add key="LocalPackages" value="~/LocalPackages" />
      </packageSources>
    </configuration>
    
  3. When you reference the package in your projects, NuGet will now look in your LocalPackages directory as well as the standard NuGet feeds.

Using Local Packages in Your Projects

Add a package reference to your project file:

<PackageReference Include="TestBae" Version="1.0.0" />

When you run dotnet restore, it will check the local feed before looking online.

Updating Local Packages

To update your local package:

  1. Increment the version in the project file:

    <PropertyGroup>
      <Version>1.0.1</Version>
    </PropertyGroup>
    
  2. Rebuild and repack the project:

    For stable releases:

    dotnet build -c Release
    dotnet pack -c Release -o [LOCAL_PACKAGES_PATH]
    

    For prerelease versions, use the --version-suffix parameter with epoch timestamp:

    # Get current epoch timestamp
    TIMESTAMP=$(date +%s)
    
    # For macOS/Linux
    dotnet build -c Release
    dotnet pack -c Release --version-suffix "beta-$TIMESTAMP" -o ~/LocalPackages
    
    # For Windows (PowerShell)
    # $timestamp = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-UFormat "%s"))
    # dotnet pack -c Release --version-suffix "beta-$timestamp" -o C:\LocalPackages
    

    This will create a package with version like 1.0.1-beta-1713887619 which includes the epoch timestamp. You can also combine predefined prefixes with the timestamp: alpha, preview, rc

    Replace [LOCAL_PACKAGES_PATH] with the appropriate path for your OS:

    • Windows: C:\LocalPackages
    • macOS/Linux: ~/LocalPackages
  3. Update your consuming projects to use the new version:

    For stable versions:

    <PackageReference Include="TestBae" Version="1.0.1" />
    

    For prerelease versions:

    <PackageReference Include="TestBae" Version="1.0.1-beta-1713887619" />
    
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 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
0.0.3 242 4/25/2025
0.0.3-beta-1745551335 170 4/25/2025
0.0.2 226 4/21/2025