Skip to content

Instantly share code, notes, and snippets.

@alanta
Last active April 10, 2020 06:35
Show Gist options
  • Select an option

  • Save alanta/17c9d86acb6307e0d83339a06ba54b08 to your computer and use it in GitHub Desktop.

Select an option

Save alanta/17c9d86acb6307e0d83339a06ba54b08 to your computer and use it in GitHub Desktop.
Generic integration test to verify all functions can be instantiated by the function host. This helps prevent problems when using the .NET Core default dependency injection container with Azure Functions.
public class When_the_function_host_is_configured
{
[Theory]
[MemberData(nameof(Functions))]
public void It_should_be_able_to_instantiate_all_functions(Type functionType)
{
// Arrange
var startup = new MyFunctions.Startup();
var host = new HostBuilder()
.ConfigureWebJobs(cfg =>
{
startup.Configure(cfg);
cfg.Services.AddTransient(functionType);
})
.Build();
// Act
var function = host.Services.GetRequiredService(functionType);
// Assert
function.Should().NotBeNull();
}
public static IEnumerable<object[]> Functions
{
get
{
return typeof(Startup)
.Assembly
.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(FunctionNameAttribute), false).Length > 0)
.Select(m => m.DeclaringType)
.Distinct()
.Select(t => new object[]
{
t
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment