Testing Guide
Comprehensive API reference for the TestHelpers SDK.
SetupFlow Method
Creates a TestFlow instance for testing your module.
public static TestFlow SetupFlow(this FlowModule module, int timeout = 10000, int numberOfOutputs = 1)
Parameters:
timeout- Maximum time to wait for operations in milliseconds (default: 10 seconds)numberOfOutputs- Number of output channels to create (default: 1)
Returns: A TestFlow instance for further configuration
Example:
var module = new RandomNumberModule();
var flow = module.SetupFlow();
Flow Control Methods
Start
Initializes and starts the module.
public static async Task<IResult> Start(this TestFlow testFlow)
Example:
var startResult = await flow.Start();
Assert.True(startResult.IsSuccess);
Stop
Stops the module gracefully.
public static async Task<IResult> Stop(this TestFlow testFlow)
Example:
var stopResult = await flow.Stop();
Assert.True(stopResult.IsSuccess);
Receive
Sends a message to the module for processing.
public static async Task Receive(this TestFlow testFlow, IFlowMessage message)
Example:
var m = new FlowMessage();
m.Set("data.A", "Hello");
m.Set("data.B", 5.0190582275390625m);
await flow.Receive(m);
Result Retrieval Methods
GetNextResult
Retrieves the next message from the module's output.
// Get next result from first output
public static async Task<IFlowMessage> GetNextResult(this TestFlow testFlow)
// Get next result from specific output
public static async Task<IFlowMessage> GetNextResult(this TestFlow testFlow, int outputModuleIndex)
Note
GetNextResult will wait for data to be sent out from the module. You can pass in many messages and process the result one by one.
Example:
var result = await flow.GetNextResult();
Assert.True(result.IsSuccess);
GetNextResults
Collects multiple results from the module's output.
// Get multiple results from first output
public static async Task<IList<IFlowMessage>> GetNextResults(this TestFlow testFlow, int numberOfResults)
// Get multiple results from specific output
public static async Task<IList<IFlowMessage>> GetNextResults(this TestFlow testFlow, int numberOfResults, int outputModuleIndex)
Example:
[Fact]
public async Task CanGetMultipleResults()
{
var module = new RandomNumberModule();
module.Settings.TargetMin = 1;
module.Settings.TargetMax = 100;
module.Settings.TargetName = "randomValue";
var flow = module.SetupFlow();
await flow.Start();
var m = new FlowMessage();
for (var i = 0; i < 10; i++)
{
await flow.Receive(m.Clone());
}
var results = await flow.GetNextResults(10);
Assert.Equal(10, results.Count);
// Verify all results have random values
foreach (var result in results)
{
Assert.True(result.Has("randomValue"));
Assert.InRange(result.Get<int>("randomValue"), 1, 99);
}
await flow.Stop();
}
GetNthResult
Retrieves a specific result by index (0-based).
// Get the nth result from first output
public static async Task<IFlowMessage> GetNthResult(this TestFlow testFlow, int index)
// Get the nth result from specific output
public static async Task<IFlowMessage> GetNthResult(this TestFlow testFlow, int index, int outputModuleIndex)
Example:
[Fact]
public async Task CanGetNthResult()
{
var module = new RandomNumberModule();
module.Settings.TargetMin = 0;
module.Settings.TargetMax = 100;
module.Settings.TargetName = "value";
var flow = module.SetupFlow(timeout: 50000);
await flow.Start();
var m = new FlowMessage();
for (var i = 0; i < 10; i++)
{
await flow.Receive(m.Clone());
}
// Get the 6th result (0-based index)
var result = await flow.GetNthResult(5);
Assert.True(result.Has("value"));
Assert.InRange(result.Get<int>("value"), 0, 99);
await flow.Stop();
}
Dependency Injection Methods
WithCredential
Adds credentials to your module for testing.
// From a credential object
flow.WithCredential(new MyCredential { Id = credentialId, ApiKey = "test-key" });
// From file
flow.WithCredential<MyCredential>("path/to/credential.json");
Example:
var credential = new DatabaseCredential
{
Id = Guid.NewGuid(),
ConnectionString = "test-connection-string"
};
var flow = module.SetupFlow()
.WithCredential(credential);
WithResource
Adds resources to your module for testing.
flow.WithResource(new MyResource { Id = resourceId, Data = "test-data" });
Example:
var resource = new FileResource
{
Id = Guid.NewGuid(),
FileName = "test-file.txt",
Data = Encoding.UTF8.GetBytes("test content")
};
var flow = module.SetupFlow()
.WithResource(resource);