ApiDocAndMock 1.0.1
See the version list below for details.
dotnet add package ApiDocAndMock --version 1.0.1
NuGet\Install-Package ApiDocAndMock -Version 1.0.1
<PackageReference Include="ApiDocAndMock" Version="1.0.1" />
<PackageVersion Include="ApiDocAndMock" Version="1.0.1" />
<PackageReference Include="ApiDocAndMock" />
paket add ApiDocAndMock --version 1.0.1
#r "nuget: ApiDocAndMock, 1.0.1"
#:package ApiDocAndMock@1.0.1
#addin nuget:?package=ApiDocAndMock&version=1.0.1
#tool nuget:?package=ApiDocAndMock&version=1.0.1
ApiMockDataFactory
A comprehensive API mocking utility for .NET projects, leveraging the power of Bogus for seamless data generation.
Installation
Install the package via NuGet:
dotnet add package ApiDocAndMock
Usage
Service Configuration
Integrate the package with your application by adding the required services:
// Main service setup
builder.Services.AddDocAndMock();
// Optional: Add authentication for API mocking
builder.Services.AddMockAuthentication();
// Swagger integration for documentation and mock APIs
builder.Services.AddMockSwagger(includeSecurity: true, includAnnotations: true);
// Optional: Add an in-memory database for simulating CRUD operations
builder.Services.AddMemoryDb();
Application Middleware
Configure the application middleware to enable API documentation and mocking:
var app = builder.Build();
// Enable API documentation and mock utilities
app.UseApiDocAndMock(useAuthentication: true, useMockOutcome: true);
// Enable Swagger for API visualization
app.UseSwagger();
app.UseSwaggerUI();
Configuration
Response Types
Customize default response types or add new ones for specific HTTP status codes:
builder.Services.AddCommonResponseConfigurations(config =>
{
config.RegisterResponseExample(500, new ProblemDetails
{
Title = "Custom Internal Server Error",
Status = 500,
Detail = "A custom error occurred. Please contact support."
});
config.RegisterResponseExample(403, new ProblemDetails
{
Title = "Forbidden",
Status = 403,
Detail = "You do not have permission to access this resource."
});
});
Default Faker Rules
Define default faker rules to override existing rules and generate mock data for specific property names:
builder.Services.AddDefaultFakerRules(rules =>
{
rules["Phone"] = faker => "+44 " + faker.Phone.PhoneNumber();
});
Mocking Configurations
Set up custom mocking configurations to apply specific rules to request and response objects. This ensures that objects adhere to predefined formats, with fallback defaults provided by Bogus.
builder.Services.AddMockingConfigurations(config =>
{
// Configure Booking object
config.RegisterConfiguration<Booking>(cfg =>
{
cfg
.ForPropertyObject<Room>("Room")
.ForPropertyObject<Contact>("PrimaryContact");
});
// Configure Hotel object
config.RegisterConfiguration<Hotel>(cfg =>
{
cfg
.ForProperty("Name", faker => faker.Company.CompanyName())
.ForPropertyObjectList<Room>("Rooms", 5)
.ForPropertyObjectList<Booking>("Bookings", 5);
});
});
Notes on Mocking
- ForPropertyObject: Applies rules to a specific nested object within a response.
- ForPropertyObjectList: Configures a list of nested objects with a specified number of items.
- If a property lacks an explicit rule, a default value is assigned based on its type. Undefined types default to
null
.
OpenApiExtensions For MinimalApi endpoints
OpenApiMockExtensions For MinimalApi Endpoints
GetMockFromMemoryDb Usage
UpdateMockWithMemoryDb Usage
CreateMockWithMemoryDb Usage
DeleteMockWithMemoryDb Usage
Product | Versions 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. |
-
net8.0
- Bogus (>= 35.6.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.11)
- Microsoft.AspNetCore.OpenApi (>= 8.0.11)
- Swashbuckle.AspNetCore (>= 7.2.0)
- Swashbuckle.AspNetCore.Annotations (>= 7.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.