| name | mock-response-factory |
| description | Add GetMockResponseFactory to API contracts and register factories in the SPA mock API service.
|
| when-to-use | GetMockResponseFactory, MockResponseFactory, MockCopicApiService, MockWebApiService, MockFactories, mock mode, IMockResponseFactory, SPA development without backend
|
Mock Response Factory
Enables Blazor SPA development without a live backend. Every API contract exposes a
factory; the SPA mock service dispatches requests to it.
Detection — find patterns in the current repo
rg -l 'GetMockResponseFactory' --glob '**/*.cs' | head -5
rg -l 'Mock.*ApiService' --glob '**/*.cs'
rg 'delegate.*MockResponseFactory' --glob '**/*.cs'
Read an existing contract + its registration before adding a new one.
Step 1 — Implement on the contract
Add to the operation's public static partial class:
public static MockResponseFactory<Response> GetMockResponseFactory()
{
return _ => new Response();
}
Rules:
- Delegate type:
MockResponseFactory<TResponse> — locate in the repo's shared contracts project
- Return plausible data, not empty shells or
default
ListResponse<T>: several varied items, set totalCount correctly
- Commands with empty
Response: return new Response() or the repo's established pattern
- Stream/file endpoints: small in-memory stream, or follow a sibling endpoint's approach
ListResponse example
public static MockResponseFactory<Response> GetMockResponseFactory()
{
AnnouncementDto[] items =
[
new() { Text = "Maintenance tonight", Link = "/announcements/1" },
new() { Text = "New feature available", Link = "/announcements/2" },
];
return _ => new Response(totalCount: items.Length, items);
}
Command example
public static MockResponseFactory<Response> GetMockResponseFactory()
{
return _ => new Response { SecurityRoleId = 1, B2CGroupId = Guid.NewGuid() };
}
Step 2 — Register in the SPA mock service
Mirror the registration pattern already in the repo. Common variants:
Dictionary on mock service:
{ typeof(GetProfile.Query), GetProfile.GetMockResponseFactory() },
{ typeof(CreateSecurityRole.Command), CreateSecurityRole.GetMockResponseFactory() },
Wrapper class per endpoint (MockFactories/ folder):
internal sealed class GetProfileMockFactory : IMockResponseFactory
{
public object Create(IApiRequest request) =>
GetProfile.GetMockResponseFactory()((GetProfile.Query)request);
}
Then register the wrapper class in the mock service's factory dictionary.
Step 3 — Verify
- SPA runs in mock mode (no backend required)
- Feature that calls the endpoint renders with sample data
- Command mocks return a response the client handler expects
Checklist
Related skills
web-api-contracts — full contract workflow; mock factory is step 10 of new endpoints