8

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)?

jootl
  • 699
  • 2
  • 8
  • 16
  • This is my exact scenario (down to the article I used to ensure my DI would be the same as the system under test). Did you ever figure out a way to do this? – Angelo Jan 22 '21 at 14:22
  • Still no, for the moment I did my integration tests on deployed environment (without mocking anything...). – jootl Jan 25 '21 at 10:00
  • Running into the same issue with a Durable Entity. Would be really nice if there's a way to test it in a CI/CD pipeline without actual deployment. Started a bounty, hopefully it will help. – Rik D Jul 08 '21 at 19:51

1 Answers1

0

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.

PramodValavala
  • 6,026
  • 1
  • 11
  • 30