DEV Community

Discussion on: Simple Dependency Injection In AWS Lambda .net core

 
gary_woodfine profile image
Gary Woodfine

Not sure I a entirely understand your question.

You can change the method to public if you want too! There is no hard approach to this. This is only an approach or pattern to use.

Besides all the classes or dependencies that would be wired up in the private method would more than likely already be unit tested.

Essentially you're just wiring up the dependencies, but if you want to unit test you've wired up your dependencies correctly then by all means change the method to public or internal and make your internals visible to your test projects.

Thread Thread
 
jamsidedown profile image
Rob Anderson

You mentioned that DI is "vitally important" to unit testing, but given that the lambda is responsible for setting up its own service provider, there's no easy way to pass a mocked ILambdaConfiguration instance into this class.

In a monolithic application, you'd normally rely on a global service provider, and a controller constructor would take an interface that could be easily mocked.

Using serverless functions removes the global environment, and I can't see why a lambda having its own internal dependency injection adds any value. It just seems to add bloat.

My question was how would you unit test a serverless function like the one in your example?

Thread Thread
 
yaser profile image
Yaser Al-Najjar

The one thing I've learned after moving to Python, is that you can do testing without mocking... just do it right not "mocked"!

Thread Thread
 
gary_woodfine profile image
Gary Woodfine

Doesn't add bloat at all. Especially when working with layers or shared assemblies.

Introducing concepts like DI actually helps quite a lot with Lambda's.

The sentence is correct, DI is vitally important to Unit Testing.

So for context. I could have a Lambda consisting of several functions listening for events on SQS Queues,

  public class EventParser
    {
        private readonly IServiceProvider _serviceProvider;

        public EventParser(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public EventParser() : this(StartUp.Container.BuildServiceProvider())
        {
        }

        [LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
        public async Task Parse(SQSEvent qEvent, ILambdaContext context)
        {
            foreach (var record in qEvent.Records)
            {
                // DO some thing here
            }
        }
    }

public class OtherEventParser
    {
        private readonly IServiceProvider _serviceProvider;

        public OtherEventParser(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public OtherEventParser() : this(StartUp.Container.BuildServiceProvider())
        {
        }

        [LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
        public async Task Parse(SQSEvent qEvent, ILambdaContext context)
        {
            foreach (var record in qEvent.Records)
            {
                // DO some thing here
            }
        }
    }

My Lambda Configuration may be configured to not only retrieve some settings from the appsettings file but also retrieve environment variables
defined in the serverless.yml

 public class LambdaConfiguration
    {
          public static IConfigurationRoot Configuration => new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                   .AddEnvironmentVariables()
                   .Build();
    }

  public class StartUp
    {
        public static IServiceCollection Container => ConfigureServices(LambdaConfiguration.Configuration);

        private static IServiceCollection ConfigureServices(IConfigurationRoot root)
        {
            var services = new ServiceCollection();
            MapConfigurationFactory.Scan<StartUp>();


            services.AddTransient<IStreamReader, DocxStreamReader>();
            services.AddHttpClient<ApiClient>( client =>
            {
                client.BaseAddress = new Uri("http://someurl.com");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            });

            services.AddTransient<IService<SomeServiceEntity>, SomeService>();
            services.AddTransient<IPostService<OtherServiceEntity>, OtherService>();

            return services;
        }
    }

What restrictions are there in me writing Unit Tests for these Lambda functions ?

Thread Thread
 
gary_woodfine profile image
Gary Woodfine

In Python Mocks are still heavily used and there are a number of Mock frameworks.

I write Lambda's in Python, and I can still implement the same unit testing strategy.

Unit Tests and Mocks are still pretty relevant in most programming frameworks. Engineering and Quality Assurance discipline remains the same irrelevant of software development language used.