Last active
April 10, 2020 06:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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