How can I use or mock IWebJobsBuilder to do an integration test of my Azure Function v2?What is the difference between integration and unit tests?How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?What's the difference between unit tests and integration tests?How to register and use different implementation of same interface?How can I use NuGet packages in my Azure Functions?Azure Webjobs vs Azure Functions : How to chooseBootstrapping TestServer with TestStartup with InMemoryDatabase fails (.Net core)Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?Azure Functions HTTP integration testing
Polynomial division: Is this trick obvious?
Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?
Would life always name the light from their sun "white"
When did Britain learn about American independence?
How was the blinking terminal cursor invented?
Why are lawsuits between the President and Congress not automatically sent to the Supreme Court
Can EU citizens work on Iceland?
Why do galaxies collide?
Find the area of the rectangle
Why is Drogon so much better in battle than Rhaegal and Viserion?
Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?
Why is the A380’s with-reversers stopping distance the same as its no-reversers stopping distance?
301 Redirects what does ([a-z]+)-(.*) and ([0-9]+)-(.*) mean
How can I safely determine the output voltage and current of a transformer?
Cannot remove door knob -- totally inaccessible!
How can we delete item permanently without storing in Recycle Bin?
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
How could it be that 80% of townspeople were farmers during the Edo period in Japan?
What is this rubber on gear cables
Pedaling at different gear ratios on flat terrain: what's the point?
Square spiral in Mathematica
Why do academics prefer Mac/Linux?
Why does the U.S military use mercenaries?
Omit property variable when using object destructuring
How can I use or mock IWebJobsBuilder to do an integration test of my Azure Function v2?
What is the difference between integration and unit tests?How can I get the application's path in a .NET console application?How can I generate random alphanumeric strings?What's the difference between unit tests and integration tests?How to register and use different implementation of same interface?How can I use NuGet packages in my Azure Functions?Azure Webjobs vs Azure Functions : How to chooseBootstrapping TestServer with TestStartup with InMemoryDatabase fails (.Net core)Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?Azure Functions HTTP integration testing
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.
public sealed class CreateAccountFunction
private readonly IAccountWorkflow m_accountWorkflow;
private readonly ILogger<CreateAccountFunction> m_logger;
private readonly IMapper m_mapper;
public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)
m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
[FunctionName("CreateAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(
AuthorizationLevel.Function,
"post",
Route = "v1/accounts/"
)]
HttpRequest httpRequest)
// Creates the account.
My Startup
class contains the following:
public sealed class Startup : IWebJobsStartup
public void Configure(IWebJobsBuilder webJobsBuilder)
webJobsBuilder.Services.AddLogging(loggingBuilder =>
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
);
var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
Now I would like to do an integration tests of the Azure Function.
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
// --> How can I reuse the Startup and IWebJobsBuilder <--
m_creationAccountFunction = new CreateAccountFunction(? ? ?);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
Question
It looks a lot of the injection stuff is handled by IWebJobsBuilder
.
How can I leverage this to do integration tests of my Azure Functions?
I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.
c# azure dependency-injection integration-testing azure-functions
add a comment |
I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.
public sealed class CreateAccountFunction
private readonly IAccountWorkflow m_accountWorkflow;
private readonly ILogger<CreateAccountFunction> m_logger;
private readonly IMapper m_mapper;
public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)
m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
[FunctionName("CreateAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(
AuthorizationLevel.Function,
"post",
Route = "v1/accounts/"
)]
HttpRequest httpRequest)
// Creates the account.
My Startup
class contains the following:
public sealed class Startup : IWebJobsStartup
public void Configure(IWebJobsBuilder webJobsBuilder)
webJobsBuilder.Services.AddLogging(loggingBuilder =>
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
);
var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
Now I would like to do an integration tests of the Azure Function.
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
// --> How can I reuse the Startup and IWebJobsBuilder <--
m_creationAccountFunction = new CreateAccountFunction(? ? ?);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
Question
It looks a lot of the injection stuff is handled by IWebJobsBuilder
.
How can I leverage this to do integration tests of my Azure Functions?
I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.
c# azure dependency-injection integration-testing azure-functions
add a comment |
I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.
public sealed class CreateAccountFunction
private readonly IAccountWorkflow m_accountWorkflow;
private readonly ILogger<CreateAccountFunction> m_logger;
private readonly IMapper m_mapper;
public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)
m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
[FunctionName("CreateAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(
AuthorizationLevel.Function,
"post",
Route = "v1/accounts/"
)]
HttpRequest httpRequest)
// Creates the account.
My Startup
class contains the following:
public sealed class Startup : IWebJobsStartup
public void Configure(IWebJobsBuilder webJobsBuilder)
webJobsBuilder.Services.AddLogging(loggingBuilder =>
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
);
var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
Now I would like to do an integration tests of the Azure Function.
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
// --> How can I reuse the Startup and IWebJobsBuilder <--
m_creationAccountFunction = new CreateAccountFunction(? ? ?);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
Question
It looks a lot of the injection stuff is handled by IWebJobsBuilder
.
How can I leverage this to do integration tests of my Azure Functions?
I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.
c# azure dependency-injection integration-testing azure-functions
I am trying to do integration tests to validate my latest Azure Functions v2 that uses the constructor dependency injection.
public sealed class CreateAccountFunction
private readonly IAccountWorkflow m_accountWorkflow;
private readonly ILogger<CreateAccountFunction> m_logger;
private readonly IMapper m_mapper;
public CreateAccountFunction(ILoggerFactory loggerFactory, IMapper mapper, IAccountWorkflow accountWorkflow)
m_logger = loggerFactory.CreateLogger<CreateAccountFunction>();
m_mapper = mapper;
m_accountWorkflow = accountWorkflow;
[FunctionName("CreateAccount")]
public async Task<IActionResult> Run(
[HttpTrigger(
AuthorizationLevel.Function,
"post",
Route = "v1/accounts/"
)]
HttpRequest httpRequest)
// Creates the account.
My Startup
class contains the following:
public sealed class Startup : IWebJobsStartup
public void Configure(IWebJobsBuilder webJobsBuilder)
webJobsBuilder.Services.AddLogging(loggingBuilder =>
loggingBuilder.SetMinimumLevel(LogLevel.Debug);
);
var mapperConfiguration = new MapperConfiguration(cfg => cfg.AddProfile(new ContractProfile()));
webJobsBuilder.Services.AddSingleton(mapperConfiguration.CreateMapper());
webJobsBuilder.Services.AddTransient<IAccountWorkflow, AccountWorkflow>();
Now I would like to do an integration tests of the Azure Function.
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
// --> How can I reuse the Startup and IWebJobsBuilder <--
m_creationAccountFunction = new CreateAccountFunction(? ? ?);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
Question
It looks a lot of the injection stuff is handled by IWebJobsBuilder
.
How can I leverage this to do integration tests of my Azure Functions?
I am looking for a solution that will minimize the need of creating custom code and reuse the existing infrastructure as much as possible.
c# azure dependency-injection integration-testing azure-functions
c# azure dependency-injection integration-testing azure-functions
asked Mar 23 at 16:16
KzrystofKzrystof
2,29231628
2,29231628
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I looked into the Azure Function host code and found this section of code in the Program.cs
file:
var host = new HostBuilder()
.SetAzureFunctionsEnvironment()
.ConfigureLogging(b =>
b.SetMinimumLevel(LogLevel.Information);
b.AddConsole();
)
.AddScriptHost(options, webJobsBuilder =>
webJobsBuilder.AddAzureStorageCoreServices();
)
.UseConsoleLifetime()
.Build();
The part that got me interested was the AddScriptHost()
extension method, which makes the webJobsBuilder
instance (an implementation of IWebJobsBuilder
) available.
Knowing that, I created the following method which creates a simple IHost
instance and uses my existing Startup
class which contains all the injected services:
/// <summary>
/// Builds an instance of the specified <typeparamref name="TFunctionType"/>
/// with the services defined in the <paramref name="startup"/> instance.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="startup"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// Thrown if:
/// - The <paramref name="startup" /> instance is not specified.
/// </exception>
public static TFunctionType Instanciate<TFunctionType>(Startup startup)
Argument.ThrowIfIsNull(startup, nameof(startup));
// --> Builds an IHost with all the services registered in the Startup.
IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();
return Instanciate<TFunctionType>(host);
The Instanciate<TFunctionType>
method looks for a constructor of TFunctionType
and retrieves all the services from the IHost
instance:
/// <summary>
/// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="host"></param>
/// <returns></returns>
private static TFunctionType Instanciate<TFunctionType>(IHost host)
Type type = typeof(TFunctionType);
// --> This part could be better...
ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();
ParameterInfo[] parametersInfo = contructorInfo.GetParameters();
object[] parameters = LookupServiceInstances(host, parametersInfo);
return (TFunctionType) Activator.CreateInstance(type, parameters);
/// <summary>
/// Gets all the parameters instances from the host's services.
/// </summary>
/// <param name="host"></param>
/// <param name="parametersInfo"></param>
/// <returns></returns>
private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)
return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
.ToArray();
I put these methods in an HostHelper
class. Now, in my test, I can reuse the Startup
class.
Even better, I can subclass Startup
so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
var startup = new Startup();
m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315785%2fhow-can-i-use-or-mock-iwebjobsbuilder-to-do-an-integration-test-of-my-azure-func%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I looked into the Azure Function host code and found this section of code in the Program.cs
file:
var host = new HostBuilder()
.SetAzureFunctionsEnvironment()
.ConfigureLogging(b =>
b.SetMinimumLevel(LogLevel.Information);
b.AddConsole();
)
.AddScriptHost(options, webJobsBuilder =>
webJobsBuilder.AddAzureStorageCoreServices();
)
.UseConsoleLifetime()
.Build();
The part that got me interested was the AddScriptHost()
extension method, which makes the webJobsBuilder
instance (an implementation of IWebJobsBuilder
) available.
Knowing that, I created the following method which creates a simple IHost
instance and uses my existing Startup
class which contains all the injected services:
/// <summary>
/// Builds an instance of the specified <typeparamref name="TFunctionType"/>
/// with the services defined in the <paramref name="startup"/> instance.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="startup"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// Thrown if:
/// - The <paramref name="startup" /> instance is not specified.
/// </exception>
public static TFunctionType Instanciate<TFunctionType>(Startup startup)
Argument.ThrowIfIsNull(startup, nameof(startup));
// --> Builds an IHost with all the services registered in the Startup.
IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();
return Instanciate<TFunctionType>(host);
The Instanciate<TFunctionType>
method looks for a constructor of TFunctionType
and retrieves all the services from the IHost
instance:
/// <summary>
/// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="host"></param>
/// <returns></returns>
private static TFunctionType Instanciate<TFunctionType>(IHost host)
Type type = typeof(TFunctionType);
// --> This part could be better...
ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();
ParameterInfo[] parametersInfo = contructorInfo.GetParameters();
object[] parameters = LookupServiceInstances(host, parametersInfo);
return (TFunctionType) Activator.CreateInstance(type, parameters);
/// <summary>
/// Gets all the parameters instances from the host's services.
/// </summary>
/// <param name="host"></param>
/// <param name="parametersInfo"></param>
/// <returns></returns>
private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)
return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
.ToArray();
I put these methods in an HostHelper
class. Now, in my test, I can reuse the Startup
class.
Even better, I can subclass Startup
so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
var startup = new Startup();
m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
add a comment |
I looked into the Azure Function host code and found this section of code in the Program.cs
file:
var host = new HostBuilder()
.SetAzureFunctionsEnvironment()
.ConfigureLogging(b =>
b.SetMinimumLevel(LogLevel.Information);
b.AddConsole();
)
.AddScriptHost(options, webJobsBuilder =>
webJobsBuilder.AddAzureStorageCoreServices();
)
.UseConsoleLifetime()
.Build();
The part that got me interested was the AddScriptHost()
extension method, which makes the webJobsBuilder
instance (an implementation of IWebJobsBuilder
) available.
Knowing that, I created the following method which creates a simple IHost
instance and uses my existing Startup
class which contains all the injected services:
/// <summary>
/// Builds an instance of the specified <typeparamref name="TFunctionType"/>
/// with the services defined in the <paramref name="startup"/> instance.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="startup"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// Thrown if:
/// - The <paramref name="startup" /> instance is not specified.
/// </exception>
public static TFunctionType Instanciate<TFunctionType>(Startup startup)
Argument.ThrowIfIsNull(startup, nameof(startup));
// --> Builds an IHost with all the services registered in the Startup.
IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();
return Instanciate<TFunctionType>(host);
The Instanciate<TFunctionType>
method looks for a constructor of TFunctionType
and retrieves all the services from the IHost
instance:
/// <summary>
/// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="host"></param>
/// <returns></returns>
private static TFunctionType Instanciate<TFunctionType>(IHost host)
Type type = typeof(TFunctionType);
// --> This part could be better...
ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();
ParameterInfo[] parametersInfo = contructorInfo.GetParameters();
object[] parameters = LookupServiceInstances(host, parametersInfo);
return (TFunctionType) Activator.CreateInstance(type, parameters);
/// <summary>
/// Gets all the parameters instances from the host's services.
/// </summary>
/// <param name="host"></param>
/// <param name="parametersInfo"></param>
/// <returns></returns>
private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)
return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
.ToArray();
I put these methods in an HostHelper
class. Now, in my test, I can reuse the Startup
class.
Even better, I can subclass Startup
so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
var startup = new Startup();
m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
add a comment |
I looked into the Azure Function host code and found this section of code in the Program.cs
file:
var host = new HostBuilder()
.SetAzureFunctionsEnvironment()
.ConfigureLogging(b =>
b.SetMinimumLevel(LogLevel.Information);
b.AddConsole();
)
.AddScriptHost(options, webJobsBuilder =>
webJobsBuilder.AddAzureStorageCoreServices();
)
.UseConsoleLifetime()
.Build();
The part that got me interested was the AddScriptHost()
extension method, which makes the webJobsBuilder
instance (an implementation of IWebJobsBuilder
) available.
Knowing that, I created the following method which creates a simple IHost
instance and uses my existing Startup
class which contains all the injected services:
/// <summary>
/// Builds an instance of the specified <typeparamref name="TFunctionType"/>
/// with the services defined in the <paramref name="startup"/> instance.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="startup"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// Thrown if:
/// - The <paramref name="startup" /> instance is not specified.
/// </exception>
public static TFunctionType Instanciate<TFunctionType>(Startup startup)
Argument.ThrowIfIsNull(startup, nameof(startup));
// --> Builds an IHost with all the services registered in the Startup.
IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();
return Instanciate<TFunctionType>(host);
The Instanciate<TFunctionType>
method looks for a constructor of TFunctionType
and retrieves all the services from the IHost
instance:
/// <summary>
/// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="host"></param>
/// <returns></returns>
private static TFunctionType Instanciate<TFunctionType>(IHost host)
Type type = typeof(TFunctionType);
// --> This part could be better...
ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();
ParameterInfo[] parametersInfo = contructorInfo.GetParameters();
object[] parameters = LookupServiceInstances(host, parametersInfo);
return (TFunctionType) Activator.CreateInstance(type, parameters);
/// <summary>
/// Gets all the parameters instances from the host's services.
/// </summary>
/// <param name="host"></param>
/// <param name="parametersInfo"></param>
/// <returns></returns>
private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)
return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
.ToArray();
I put these methods in an HostHelper
class. Now, in my test, I can reuse the Startup
class.
Even better, I can subclass Startup
so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
var startup = new Startup();
m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
I looked into the Azure Function host code and found this section of code in the Program.cs
file:
var host = new HostBuilder()
.SetAzureFunctionsEnvironment()
.ConfigureLogging(b =>
b.SetMinimumLevel(LogLevel.Information);
b.AddConsole();
)
.AddScriptHost(options, webJobsBuilder =>
webJobsBuilder.AddAzureStorageCoreServices();
)
.UseConsoleLifetime()
.Build();
The part that got me interested was the AddScriptHost()
extension method, which makes the webJobsBuilder
instance (an implementation of IWebJobsBuilder
) available.
Knowing that, I created the following method which creates a simple IHost
instance and uses my existing Startup
class which contains all the injected services:
/// <summary>
/// Builds an instance of the specified <typeparamref name="TFunctionType"/>
/// with the services defined in the <paramref name="startup"/> instance.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="startup"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// Thrown if:
/// - The <paramref name="startup" /> instance is not specified.
/// </exception>
public static TFunctionType Instanciate<TFunctionType>(Startup startup)
Argument.ThrowIfIsNull(startup, nameof(startup));
// --> Builds an IHost with all the services registered in the Startup.
IHost host = new HostBuilder().ConfigureWebJobs(startup.Configure).Build();
return Instanciate<TFunctionType>(host);
The Instanciate<TFunctionType>
method looks for a constructor of TFunctionType
and retrieves all the services from the IHost
instance:
/// <summary>
/// Instanciates the specified <typeparamref name="TFunctionType"></typeparamref>.
/// </summary>
/// <typeparam name="TFunctionType"></typeparam>
/// <param name="host"></param>
/// <returns></returns>
private static TFunctionType Instanciate<TFunctionType>(IHost host)
Type type = typeof(TFunctionType);
// --> This part could be better...
ConstructorInfo contructorInfo = type.GetConstructors().FirstOrDefault();
ParameterInfo[] parametersInfo = contructorInfo.GetParameters();
object[] parameters = LookupServiceInstances(host, parametersInfo);
return (TFunctionType) Activator.CreateInstance(type, parameters);
/// <summary>
/// Gets all the parameters instances from the host's services.
/// </summary>
/// <param name="host"></param>
/// <param name="parametersInfo"></param>
/// <returns></returns>
private static object[] LookupServiceInstances(IHost host, IReadOnlyList<ParameterInfo> parametersInfo)
return parametersInfo.Select(p => host.Services.GetService(p.ParameterType))
.ToArray();
I put these methods in an HostHelper
class. Now, in my test, I can reuse the Startup
class.
Even better, I can subclass Startup
so that I can mock pieces of code that uses some kind of I/O to make my integration tests more resilient:
public class CreateAccountFunctionTests
private readonly CreateAccountFunction m_creationAccountFunction;
public CreateAccountFunctionTests()
var startup = new Startup();
m_creationAccountFunction = HostHelper.Instanciate<CreateAccountFunction>(startup);
[Fact]
public void TestSomething()
// Arrange.
HttpRequest httpRequest = /* builds an instance of HttpRequest */
// Act.
var result = m_creationAccountFunction.Run(httpRequest);
// Assert.
// Asserts the Status Code.
edited Mar 24 at 16:22
answered Mar 24 at 14:51
KzrystofKzrystof
2,29231628
2,29231628
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315785%2fhow-can-i-use-or-mock-iwebjobsbuilder-to-do-an-integration-test-of-my-azure-func%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown