Portal Community

Mock Service Registry

// Tests/Mocks/MockBusinessServiceRegistry.cs
public class MockBusinessServiceRegistry : IBusinessServiceRegistry
{
    private readonly Dictionary<string, Func<object, object>> _handlers = new();

    public MockBusinessServiceRegistry OnCall(string serviceId, string operationId, Func<object, object> handler)
    {
        _handlers[$"{serviceId}:{operationId}"] = handler;
        return this;
    }

    public Task<string> ResolveEndpointAsync(string serviceId, string operationId)
        => Task.FromResult($"http://mock/{serviceId}/{operationId}");

    public Task<bool> IsHealthyAsync(string serviceId) => Task.FromResult(true);
}

Unit Test Example

[Test]
public async Task BusinessServiceCall_CalculatesPayslip_ReturnsNetPay()
{
    // Arrange
    var mockRegistry = new MockBusinessServiceRegistry()
        .OnCall("payroll-service", "calculatePayslip", input => new
        {
            data = new { payslipId = "ps-001", grossPay = 5000, netPay = 3850 }
        });

    var mockAuthProvider = new Mock<IBusinessServiceAuthProvider>();
    mockAuthProvider.Setup(a => a.GetAuthHeadersAsync("payroll-service", default))
        .ReturnsAsync(new Dictionary<string, string>());

    var executor = new BusinessServiceCallExecutor(mockRegistry, mockAuthProvider.Object);
    var ctx = NodeExecutionContext.ForTest(new
    {
        serviceId = "payroll-service",
        operationId = "calculatePayslip",
        inputMap = new { employeeId = "emp-001" }
    });

    // Act
    var result = await executor.ExecuteAsync(ctx);

    // Assert
    Assert.AreEqual("ps-001", result.Output.GetString("data.payslipId"));
    Assert.AreEqual(3850m, result.Output.GetDecimal("data.netPay"));
}

Pinned Data for Test Replay

In the Flow Studio designer, use the Node Inspector's "Set Pinned Data" feature to capture a real service response and replay it during subsequent test runs without hitting the live service. This is the recommended workflow for testing complex multi-node workflows that depend on business services.

Integration tests: For full integration tests, register a mock HTTP server (e.g., WireMock.Net) instead of the real service URL. Set the service's BaseUrl to the mock server in your test environment config.