Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active August 15, 2019 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/fcfaf0922513c661b63623eb04cccebd to your computer and use it in GitHub Desktop.
Save justinyoo/fcfaf0922513c661b63623eb04cccebd to your computer and use it in GitHub Desktop.
Revising Azure Functions Dependency Injection
public class AppModule : Module
{
public override void Load(IServiceCollection services)
{
services.AddSingleton<AppSettings>();
services.AddTransient<IGetSamplesFunction, GetSamplesFunction>();
}
}
[TestMethod]
public async Task Given_Request_Response_Should_Return_Result()
{
var logger = new Mock<ILogger>();
var function = new Mock<IGetSamplesFunction>();
function.SetupProperty(p => p.Log, logger.Object);
var result = new ObjectResult("hello world");
function.Setup(p => p.InvokeAsync<HttpRequest, IActionResult>(
It.IsAny<HttpRequest>(),
It.IsAny<FunctionOptionsBase>()))
.ReturnsAsync(result);
var trigger = new SampleHttpTrigger2(function.Object);
var req = new Mock<HttpRequest>();
var log = new Mock<ILogger>();
var response = await trigger.GetSamples2(req.Object, log.Object).ConfigureAwait(false);
response.Should().BeOfType<ObjectResult>();
var @return = response as ObjectResult;
@return.Value.Should().Be("hello world");
}
public static class SampleHttpTrigger1
{
public static IFunctionFactory Factory { get; set; } = new FunctionFactory(new AppModule());
[FunctionName(nameof(GetSamples1))]
public static async Task<IActionResult> GetSamples1(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "samples1")] HttpRequest req,
ILogger log)
{
var options = new GetSamplesFunctionOptions("sample 1");
var result = await Factory.Create<IGetSamplesFunction, ILogger>(log)
.InvokeAsync<HttpRequest, IActionResult>(req, options)
.ConfigureAwait(false);
return result;
}
}
public class SampleHttpTrigger2
{
private readonly IGetSamplesFunction _function;
public SampleHttpTrigger2(IGetSamplesFunction function)
{
this._function = function ?? throw new ArgumentNullException(nameof(function));
}
[FunctionName(nameof(GetSamples2))]
public async Task<IActionResult> GetSamples2(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "samples2")] HttpRequest req,
ILogger log)
{
this._function.Log = log;
var options = new GetSamplesFunctionOptions("sample 2");
var result = await this._function
.InvokeAsync<HttpRequest, IActionResult>(req, options)
.ConfigureAwait(false);
return result;
}
}
public class SampleHttpTrigger
{
[FunctionName(nameof(GetSamples))]
public async Task<IActionResult> GetSamples(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "samples")] HttpRequest req,
ILogger log)
{
var name = req.Query["name"];
return new OkObjectResult($"Hello {name}");
}
}
// Registger assembly
[assembly: WebJobsStartup(typeof(StartUp))]
namespace Sample.FunctionApp
{
// Implement IWebJobStartup interface.
public class StartUp : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<AppSettings>();
builder.Services.AddTransient<IGetSamplesFunction, GetSamplesFunction>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment