Durable Azure Functions and Integration Tests

Viewed 1109

I'm trying to test (integration tests) my Azure Durable Function v3 with SpecFlow and MSTest.

Functions are initialized with DI and Startup class:

[assembly: FunctionsStartup(typeof(Startup))]
namespace MyNamespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            ConfigureServices(builder.Services);
        }
...

Orchestrator entry point is triggered by HTTP endpoint:

public class Function1
{
    [FunctionName(nameof(Function1))]
    public async Task<IActionResult> DoYourJob(
        [HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", Route = "api/routes/function1")] HttpRequestMessage httpRequest,
        [DurableClient] IDurableOrchestrationClient starter)
        {
...

My IntegrationTest constructor initializes Az Function with HostBuilder (thanks to this article):

[Binding]
public sealed class Function1StepDefinitions
{
    private readonly IHost _host;
    private readonly Function1 _function;

    public Function1StepDefinitions()
    {
        var startup = new MyNamespace.Functions.Startup();
        _host = new HostBuilder()
            .ConfigureWebJobs(config =>
            {
                config.AddDurableTask(options =>
                {
                    options.HubName = "MyTaskHub";
                });

                startup.Configure(config);
            })
            .ConfigureServices(ReplaceTestOverrides)  // Method to replace/mock some external services
            .Build();

        _function = new Function1(Host.Services.GetRequiredService<IOptions..., ..);

And in my test I dont know how to retrieve IDurableOrchestrationClient to call my HTTP Trigger:

    [When(@"I call my function")]
    public async Task WhenICallMyFunction()
    {
        var request = new HttpRequestMessage();

        await _function.DoYourJob(
            request,
            Host.Services.GetService<IDurableOrchestrationClient>()); // This is always null
...

Is there a way to retrieve this IDurableOrchestrationClient and test my whole Function (with Orchestrator and Activities calls)?

1 Answers

Looks like there is a IDurableClientFactory that you can inject instead based on this code.

Using this, you can create a client to use as shown in this sample.

Related