Mockify 1.2.1

dotnet add package Mockify --version 1.2.1
                    
NuGet\Install-Package Mockify -Version 1.2.1
                    
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="Mockify" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mockify" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="Mockify" />
                    
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 Mockify --version 1.2.1
                    
#r "nuget: Mockify, 1.2.1"
                    
#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 Mockify@1.2.1
                    
#: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=Mockify&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=Mockify&version=1.2.1
                    
Install as a Cake Tool

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 call AddControllers(options => ...) once only.

  • 204 No Content returned
    The action has [MockData] but didn’t generate any object. Ensure MockDataFilter calls OkObjectResult.

  • ArgumentException: int cannot convert to Guid
    Ensure generator creates type-compatible values (special handling for Guid, enums, etc.).

  • Endpoints duplicated (mock repeated)
    Ensure AddMockPrefix 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 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. 
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.2.1 190 9/21/2025
1.2.0 188 9/21/2025
1.1.0 266 9/17/2025
1.0.1 277 9/16/2025
1.0.0 275 9/16/2025