Skip to content

Create a Mock (Code)

This sample builds the same mock as the YAML quick start, but assembles it directly in Program.cs.

The completed sample is available at DummyAppMock (Code).

Create the Project

dotnet new qaas-mocker -n DummyAppMock
cd DummyAppMock
dotnet add DummyAppMock/DummyAppMock.csproj package QaaS.Mocker --version 2.0.5
dotnet add DummyAppMock/DummyAppMock.csproj package QaaS.Common.Generators --version 3.1.2

Add the Response File

DummyAppMock/ServerData/sample.json

{
  "message": "hello from DummyAppMock",
  "source": "dummy_app_mock"
}

Reuse the Same Local Processor

Use the same ServerDataProcessor shown in the YAML quick start. The code-first sample keeps the processor identical so the only difference is how the mock is configured.

Build Program.cs in Small Pieces

DummyAppMock/Program.cs

Start with the Imports

These are the only namespaces the sample needs: the local processor, the FromFileSystem generator, the Mocker builder surface, and the HTTP server configuration types.

using DummyAppMock.Processors;
using QaaS.Common.Generators.ConfigurationObjects.FromExternalSourceConfigurations;
using QaaS.Common.Generators.FromExternalSourceGenerators;
using QaaS.Framework.SDK.DataSourceObjects;
using QaaS.Mocker;
using QaaS.Mocker.Servers.ConfigurationObjects;
using QaaS.Mocker.Servers.ConfigurationObjects.HttpServerConfigs;
using QaaS.Mocker.Stubs.ConfigurationObjects;
using HttpMethod = QaaS.Mocker.Servers.ConfigurationObjects.HttpServerConfigs.HttpMethod;

Create the Data Source

Start by describing where the response body comes from. This block gives the mock a named data source called ServerData that reads files from the local ServerData folder.

var dataSource = new DataSourceBuilder()
    .Named("ServerData")
    .HookNamed(nameof(FromFileSystem))
    .Configure(new FromFileSystemConfig
    {
        DataArrangeOrder = DataArrangeOrder.AsciiAsc,
        FileSystem = new FileSystemConfig
        {
            Path = "ServerData"
        }
    });

Create the Stub

Next connect the incoming request flow to the local processor. The stub says "when this action runs, execute ServerDataProcessor and give it the ServerData source."

var stub = new TransactionStubBuilder()
    .Named("ServerDataStub")
    .HookNamed(nameof(ServerDataProcessor))
    .CreateDataSourceName("ServerData");

Create the Server

Now define the mock surface itself. This server listens on port 8080 and maps GET /data to the stub you just created.

var server = new ServerConfig
{
    Http = new HttpServerConfig
    {
        Port = 8080,
        IsLocalhost = false,
        Endpoints =
        [
            new HttpEndpointConfig
            {
                Path = "/data",
                Actions =
                [
                    new HttpEndpointActionConfig
                    {
                        Name = "GetServerData",
                        Method = HttpMethod.Get,
                        TransactionStubName = "ServerDataStub"
                    }
                ]
            }
        ]
    }
};

Assemble the Mocker Execution

Finally attach each piece to the current execution builder. CreateServer(...) appends the server to the builder's current server list, which is the documented code-first API to use when you want to grow the Servers collection.

var executionBuilder = new ExecutionBuilder()
    .CreateDataSource(dataSource)
    .CreateStub(stub)
    .CreateServer(server);

new MockerRunner(executionBuilder).Run();

Full Program.cs

This is the complete file exactly as it appears in the sample repository.

DummyAppMock/Program.cs

using DummyAppMock.Processors;
using QaaS.Common.Generators.ConfigurationObjects.FromExternalSourceConfigurations;
using QaaS.Common.Generators.FromExternalSourceGenerators;
using QaaS.Framework.SDK.DataSourceObjects;
using QaaS.Mocker;
using QaaS.Mocker.Servers.ConfigurationObjects;
using QaaS.Mocker.Servers.ConfigurationObjects.HttpServerConfigs;
using QaaS.Mocker.Stubs.ConfigurationObjects;
using HttpMethod = QaaS.Mocker.Servers.ConfigurationObjects.HttpServerConfigs.HttpMethod;

var dataSource = new DataSourceBuilder()
    .Named("ServerData")
    .HookNamed(nameof(FromFileSystem))
    .Configure(new FromFileSystemConfig
    {
        DataArrangeOrder = DataArrangeOrder.AsciiAsc,
        FileSystem = new FileSystemConfig
        {
            Path = "ServerData"
        }
    });

var stub = new TransactionStubBuilder()
    .Named("ServerDataStub")
    .HookNamed(nameof(ServerDataProcessor))
    .CreateDataSourceName("ServerData");

var server = new ServerConfig
{
    Http = new HttpServerConfig
    {
        Port = 8080,
        IsLocalhost = false,
        Endpoints =
        [
            new HttpEndpointConfig
            {
                Path = "/data",
                Actions =
                [
                    new HttpEndpointActionConfig
                    {
                        Name = "GetServerData",
                        Method = HttpMethod.Get,
                        TransactionStubName = "ServerDataStub"
                    }
                ]
            }
        ]
    }
};

var executionBuilder = new ExecutionBuilder()
    .CreateDataSource(dataSource)
    .CreateStub(stub)
    .CreateServer(server);

new MockerRunner(executionBuilder).Run();

This Program.cs maps directly to the YAML quick start: one FromFileSystem data source, one stub, and one HTTP server endpoint.

Run the Code Path

From DummyAppMock/DummyAppMock:

dotnet run

Then verify it:

curl http://127.0.0.1:8080/data

The mock keeps running after the check. Stop it with Ctrl+C when you are done.