Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dependency Injections on Azure Functions V2
public class CoreGitHubRepositoriesHttpTriggerTests
public async Task Given_TypeAndName_Run_Should_Return_Result()
// Arrange
var result = new { Hello = "World" };
var function = new Mock<IGitHubRepositoriesFunction>();
function.Setup(p => p.InvokeAsync<HttpRequest, object>(It.IsAny<HttpRequest>(), It.IsAny<FunctionOptionsBase>())).ReturnsAsync(result);
var factory = new Mock<IFunctionFactory>();
factory.Setup(p => p.Create<IGitHubRepositoriesFunction>(It.IsAny<ILogger>())).Returns(function.Object);
// Inject the mocked IFunctionFactory instance.
CoreGitHubRepositoriesHttpTrigger.Factory = factory.Object;
var query = new FakeQueryCollection();
query["type"] = "lorem";
query["name"] = "ipsum";
var req = new Mock<HttpRequest>();
req.SetupGet(p => p.Query).Returns(query);
var log = new Mock<ILogger>();
// Action
// Inject the mocked parameters.
var response = await CoreGitHubRepositoriesHttpTrigger.Run(req.Object, log.Object).ConfigureAwait(false);
// Assert
(response as OkObjectResult).Value.Should().Be(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment