Mockify 1.2.1
dotnet add package Mockify --version 1.2.1
NuGet\Install-Package Mockify -Version 1.2.1
<PackageReference Include="Mockify" Version="1.2.1" />
<PackageVersion Include="Mockify" Version="1.2.1" />
<PackageReference Include="Mockify" />
paket add Mockify --version 1.2.1
#r "nuget: Mockify, 1.2.1"
#:package Mockify@1.2.1
#addin nuget:?package=Mockify&version=1.2.1
#tool nuget:?package=Mockify&version=1.2.1
Mockify
Overview
Mockify is a lightweight .NET library for generating mock data and exposing mock endpoints automatically.
It is designed to:
- Generate fake data for DTOs/entities (
Generate<T>
,GenerateList<T>
). - Provide mock endpoints (
/mock/...
) for any API action decorated with[MockData]
. - Allow flexible configuration (count, min/max, seed, custom values, callbacks, global dictionaries, JSON loading).
- Log all registered mock endpoints into
mock_endpoints.txt
for easy discovery.
Installation
From NuGet:
dotnet add package Mockify --version 1.0.0
Or locally during development:
dotnet pack -c Release
dotnet nuget add source "C:\local-nugets" -n local
dotnet add package Mockify --version 1.0.0
Quick Start
1. Configure in Program.cs
using Mockify.Features;
var builder = WebApplication.CreateBuilder(args);
// Add controllers with filters & conventions
builder.Services.AddControllers(options =>
{
options.Filters.Add<MockDataFilter>(); // enable mock filter
options.Conventions.Add(new MockDataRouteConvention()); // add mock routes
});
// Enable or disable mocks via configuration
bool enableMocks = builder.Configuration.GetValue<bool>("EnableMocks");
MockDataGenerator.Configure(enableMocks, "en"); // "ar" for Arabic
// Swagger (optional)
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
1. appsettings.json
using this json field you can run or hold all mocks 😉
{
"EnableMocks": true,
}
2. Example Controller
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
// Original route: /api/Products/GetAll
// Mock route: /mock/api/Products/GetAll
[HttpGet("GetAll")]
[MockData(Count = 10)]
public IEnumerable<ProductDto> GetAll() => Enumerable.Empty<ProductDto>();
// Custom route example
// Original route: /api/Products/custom-path
// Mock route: /mock/custom-path
[HttpGet("custom-path")]
[MockData]
public ProductDto Custom() => new ProductDto();
}
New Feature 😃
-Now in v1.2.0 you can use [MockData] on class level like this:
[MockData(Count = 5)]
public class ProductsController : ControllerBase
{
// Original route: /api/Products/GetAll
// Mock route: /mock/api/Products/GetAll
[HttpGet("GetAll")]
public IEnumerable<ProductDto> GetAll() => Enumerable.Empty<ProductDto>();
// Custom route example
// Original route: /api/Products/custom-path
// Mock route: /mock/custom-path
[HttpGet("custom-path")]
public ProductDto Custom() => new ProductDto();
}
-when you use [MockData] on Controller the mock will be create Mocks for all actions inside this controller.
if the entity ProductDto
} have any collections or lists you should add [MockData] to this collection like this:
public class ProductDto
{
public Guid Id { get; set; }
public string Name { get; set; } = "";
public string Industry { get; set; } = "";
public byte[] SSTest { get; set; }
[MockData(Count =3, Min =1 , Max =5)]
public List<OrderDto> Orders { get; set; } = new();
}
Core APIs
MockDataAttribute
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Class)]
public class MockDataAttribute : Attribute
{
public string MockName { get; set; } = "mock";
public string MethodName { get; set; } = "";
public bool Enabled { get; set; } = true;
public int Count { get; set; } = 1;
public int Min { get; set; } = 0;
public int Max { get; set; } = 0;
public int? Seed { get; set; } = null;
public Type? TargetType { get; set; }
public string[]? CustomValues { get; set; }
}
MockDataGenerator
// Initialize
MockDataGenerator.Configure(
enable: true,
customKeywords: new[] { "name", "email" },
customData: new Dictionary<string,string[]>{ ["name"] = new[] { "ahmed","mohamed" } },
customCallbacks: new Dictionary<string,Func<object>>{ ["id"] = () => Guid.NewGuid() },
profile: "Development",
verbose: false
);
// Generate objects
var item = MockDataGenerator.Generate<MyDto>();
var list = MockDataGenerator.GenerateList<MyDto>(10);
// Load global dictionary from JSON file
MockDataGenerator.LoadGlobalDataFromJson("mock-data.json");
Fixes & Updates in v1.1.0
✅ Support Post, Put, Delete methods.
✅ Support for nullable types.
✅ Inheritance support → properties from base classes are now generated.
✅ Nested DTO handling → collections and child objects respect [MockData] attributes.
✅ Min/Max numeric ranges respected for int, double, and decimal.
✅ GlobalData + Callbacks take precedence when provided.
✅ Fixed empty/204 issue → ensures generated object returns instead of null.
✅ Improved enum generation with weighted random selection.
✅ Added JSON global data loader for custom datasets.
Fixes & Updates in v1.2.0
✅ The MockName
property has been added to the MockDataAttribute
to control the Route Path.
✅ The MethodName
property has been added to the MockDataAttribute
to control the Route Path.
✅ Improved logging of mock endpoints to mock_endpoints.txt
.
✅ Enhanced error handling and validation for type compatibility.
✅ Added support for byte[]
properties with random byte generation.
✅ Improved performance for large list generation.
Mock Routing
- If an action has
[HttpGet("custom")]
, the mock route will be/mock/custom
. - If no template is provided, fallback is
/mock/{Controller}/{Action}
. MockDataFilter
intercepts/mock/...
calls and returns fake data instead of executing the actual method.
Endpoint Logging
Every mock endpoint is logged into mock_endpoints.txt
inside AppContext.BaseDirectory
.
This allows you to quickly verify all available mock routes.
Troubleshooting
Mock routes not working (
/mock/...
)
Ensure you callAddControllers(options => ...)
once only.204 No Content returned
The action has[MockData]
but didn’t generate any object. EnsureMockDataFilter
callsOkObjectResult
.ArgumentException: int cannot convert to Guid
Ensure generator creates type-compatible values (special handling forGuid
, enums, etc.).Endpoints duplicated (mock repeated)
EnsureAddMockPrefix
generates full path only once.
Local Development
dotnet pack -c Release
dotnet nuget push bin\Release\Mockify.*.nupkg -k <API_KEY> -s https://api.nuget.org/v3/index.json
GitHub Actions for CI/CD
name: Publish NuGet
on:
push:
tags:
- 'v*'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- run: dotnet build -c Release
- run: dotnet pack -c Release -o ./nupkgs
- run: dotnet nuget push ./nupkgs/*.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json
Versioning
Follow Semantic Versioning:
- MAJOR: breaking changes
- MINOR: new features, backwards-compatible
- PATCH: bug fixes
Sample Program.cs
using Mockify.Features;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddControllers(options =>
{
options.Filters.Add<MockDataFilter>();
options.Conventions.Add(new MockDataRouteConvention());
});
bool enableMocks = builder.Configuration.GetValue<bool>("EnableMocks");
MockDataGenerator.Configure(enableMocks, "en"); // "ar" for Arabic
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
âš¡ Now you have a ready-to-go mock data generator + mock API endpoints with zero setup.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Bogus (>= 35.6.3)
- MH.Conversions (>= 1.0.0)
- Microsoft.AspNetCore.Mvc (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.