TestBae 0.0.2
See the version list below for details.
dotnet add package TestBae --version 0.0.2
NuGet\Install-Package TestBae -Version 0.0.2
<PackageReference Include="TestBae" Version="0.0.2" />
<PackageVersion Include="TestBae" Version="0.0.2" />
<PackageReference Include="TestBae" />
paket add TestBae --version 0.0.2
#r "nuget: TestBae, 0.0.2"
#:package TestBae@0.0.2
#addin nuget:?package=TestBae&version=0.0.2
#tool nuget:?package=TestBae&version=0.0.2
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
- Right-click on your project in Solution Explorer
- Select "Manage NuGet Packages..."
- Switch to the "Browse" tab
- Search for "TestBae"
- 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:
Build the project in Release mode:
dotnet build -c ReleasePack 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
- Windows:
This will create a .nupkg file in your LocalPackages directory.
Setting Up Local NuGet Feed
Create a LocalPackages directory in a location accessible to all your projects:
Windows:
mkdir C:\LocalPackagesmacOS/Linux:
mkdir ~/LocalPackagesEdit or create a
nuget.configfile 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>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:
Increment the version in the project file:
<PropertyGroup> <Version>1.0.1</Version> </PropertyGroup>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:\LocalPackagesThis will create a package with version like
1.0.1-beta-1713887619which includes the epoch timestamp. You can also combine predefined prefixes with the timestamp: alpha, preview, rcReplace
[LOCAL_PACKAGES_PATH]with the appropriate path for your OS:- Windows:
C:\LocalPackages - macOS/Linux:
~/LocalPackages
- Windows:
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 | Versions 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. |
-
net6.0
- AutoFixture.AutoMoq (>= 4.17.0)
- AutoFixture.Community.AutoMapper (>= 1.1.0)
- AutoMapper (>= 11.0.1)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Moq (>= 4.17.2)
- xunit (>= 2.4.1)
-
net8.0
- AutoFixture.AutoMoq (>= 4.17.0)
- AutoFixture.Community.AutoMapper (>= 1.1.0)
- AutoMapper (>= 11.0.1)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 11.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Moq (>= 4.17.2)
- xunit (>= 2.4.1)
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 |