Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
412 stars 176 forks source link

Guidance / modification for mocking / testing middleware GetInvocationResult<T> #1137

Open kfallstrom opened 1 year ago

kfallstrom commented 1 year ago

So far, we have been successful at building dotnet-isolated middleware, but are trying to write unit tests to cover our Middleware's output, and the protection level for IFunctionBindingsFeature seems to be the main issue.

https://github.com/Azure/azure-functions-dotnet-worker/blob/3b4ae8c22254e63ea21b670d8d95012f77770e15/src/DotNetWorker.Core/Context/FunctionContextFunctionBindingExtensions.cs#L17

Even with Mocks and TestDoubles copied from the current branch, the tests fail whenever I use the extension context.GetInvocationResult<>() this particular method is impossible to mock/override, as IFunctionBindingsFeature is inaccessible and can't be referenced:

IInvocationFeatures.Get<IFunctionBindingsFeature>()

Could this interface's protection be loosened or a TestHelper be provided?

Very happy with the isolated process middleware, but want to cover it with tests.

gwmensink commented 1 year ago

Any updates on this ticket? I'm facing the exact same issue. In my specific scenario I am using the GetHttpRequestDataAsync extension method which internally triggers the IInvocationFeatures.Get<IFunctionBindingsFeature>() call.

public async  Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
 {
        var httpRequest = await context.GetHttpRequestDataAsync();
        ....
WestDiscGolf commented 1 year ago

Looking at how to unit test middleware as well and the dependency on FunctionContext is causing my head to hurt. I've looked at the repo for guidance and can't find an example. All the FunctionContext test helpers are internal 😢

Please provide guidance on how to do this. If .NET 7 Isolated Functions are the future we need to be able to unit test them easily as well as the middleware which goes with them.

Thanks

adeliab commented 1 year ago

I'm also creating an ExceptionHandlingMiddleware based on this example: https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/CustomMiddleware/ExceptionHandlingMiddleware.cs

But have some logic there that we would like to unit tests. But I can't mock GetHttpRequestDataAsync() and internally that calls another extension method. Is there an example that mocks GetHttpRequestDataAsync() and GetInvocationResult()?

We should be able to unit test the middleware but these implementations make it difficult. How is this issue still not get an answer since October last year?

jepetty commented 2 months ago

+1 - Making IFunctionBindingsFeature public would be enough to unblock. As of now, I have to create an interface wrapper for this single call . It's annoying and not very pretty :(

Barsonax commented 1 month ago

Any progress on this? I find it weird that in 2024 testing is not a first class citizen of a framework.