Azure / azure-functions-dotnet-worker

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

How to mock FunctionContext for unit test? #304

Open kenakamu opened 3 years ago

kenakamu commented 3 years ago

I tried to search sample or code which explains how to properly unit test isolated process version of Function. Any input are appreciated!

fabiocav commented 3 years ago

Great question.

FunctionContext is an abstract class, so you can leverage mock frameworks or implement your test version (one example may be found here ).

We also have plans to release a set of test helpers/test framework for the Worker, but we don't currently have an ETA on that.

fabiocav commented 3 years ago

/cc @brettsam

kenakamu commented 3 years ago

Great! Thanks. I will follow the sample to see if I can implement unit test :)

SeanFeldman commented 3 years ago

We also have plans to release a set of test helpers/test framework for the Worker, but we don't currently have an ETA on that.

@fabiocav is there a tracking issue for the feature/work to be done? Thank you.

BrandonSchreck commented 3 years ago

We also have plans to release a set of test helpers/test framework for the Worker, but we don't currently have an ETA on that.

@fabiocav is there a tracking issue for the feature/work to be done? Thank you.

Are there any updates for the test helpers/test framework? I'm struggling to setup unit tests around classes that implement IFunctionsWorkerMiddleware, especially with FunctionContext.

Stef16Robbe commented 3 years ago

Bump on the updates? :)

kosperera commented 2 years ago

Does anyone has any samples or reference docs about How to write unit tests for middleware using mstests v2 and moq for azure functions v4 .net6 isolated?

zebslc commented 2 years ago

I have spent a number of days trying to test this. It has involved frequently going back to the source code because every time I think I have something mocked, I discover yet another private interface. In the absence of a good testing guide you really have made this as opaque and difficult as possible 🤬. It has now been over a year and still this seems the only real guide! Is there a better resource available. I am particularly struggling with how to mock FunctionContext and set the bindings and features. Would really appreciate if you could point me to some actual complex samples that don't involve all your internal interfaces.

lwyj123 commented 1 year ago

any updates? currently the old azure function 3.x is being deprecated. We need a mock solution for FunctionContext

stevendarby commented 1 year ago

Guidance on how to mock FunctionContext so that the BindInputAsync extension method works would be appreciated

iqoOopi commented 10 months ago

Great question.

FunctionContext is an abstract class, so you can leverage mock frameworks or implement your test version (one example may be found here ).

We also have plans to release a set of test helpers/test framework for the Worker, but we don't currently have an ETA on that.

so many internal classes in the repo, so others can't do the same thing... the linked example will only work in your SDK

Fazer01 commented 8 months ago

Following as we are experiencing the same issues as described here! Are there any updates whatsoever, @fabiocav and @brettsam?

Fazer01 commented 7 months ago

A possible solution may be as mentioned here

Benjlet commented 2 months ago

In case this is of use to others, I have created a basic HttpRequestData builder for .NET 8:

This is based on a few similar issues and respositories, fulfilling some of the simpler use cases for integration/Moq testing.

Anyone is welcome to use/adapt/copy whatever areas may help you.