Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Azure Functions unit testing sample #312

Merged
merged 9 commits into from
May 17, 2024
Merged

Conversation

davidmrdavid
Copy link
Member

We've received a few requests to provide unit testing samples for Azure Functions. This PR provides a minimal example of how to set up such tests.

In general, I had no issue mocking the DF APIs except for the CreateReplaySafeLogger API, and the Azure Functions worker GetLogger API, both of which are not marked as virtual or abstract, and therefore cannot be mocked. Not sure what's the guidance for dealing with those APIs, perhaps it's just a gap in the unit testing experience today. As a result, I removed the logger usage in the Azure Functions samples, so that the methods there could be unit tested in their entirerty.

To help bridge the gap on the DF-side, I marked CreateReplaySafeLogger as virtual. On the Functions worker side, we may need guidance or assistance.

@davidmrdavid davidmrdavid requested a review from jviau May 14, 2024 15:56
@davidmrdavid davidmrdavid requested a review from jviau May 17, 2024 03:11
Comment on lines 18 to 19
ILogger logger = context.CreateReplaySafeLogger(nameof(AzureFunctionsApp));
logger.LogInformation("Saying hello.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add this back right after the change to make CreateReplaySafeLogger virtual is released.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be needed if you use Moq.Protected?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, added it back just now

Microsoft.DurableTask.sln Outdated Show resolved Hide resolved
davidmrdavid and others added 4 commits May 17, 2024 10:23
Co-authored-by: Jacob Viau <javia@microsoft.com>
Co-authored-by: Jacob Viau <javia@microsoft.com>
Co-authored-by: Jacob Viau <javia@microsoft.com>
Comment on lines 18 to 19
ILogger logger = context.CreateReplaySafeLogger(nameof(AzureFunctionsApp));
logger.LogInformation("Saying hello.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be needed if you use Moq.Protected?

samples/AzureFunctionsUnitTests/SampleUnitTests.cs Outdated Show resolved Hide resolved
davidmrdavid and others added 2 commits May 17, 2024 10:48
@@ -385,7 +385,7 @@ public virtual Task CallSubOrchestratorAsync(TaskName orchestratorName, TaskOpti
/// </summary>
/// <param name="categoryName">The logger's category name.</param>
/// <returns>An instance of <see cref="ILogger"/> that is replay-safe.</returns>
public ILogger CreateReplaySafeLogger(string categoryName)
public virtual ILogger CreateReplaySafeLogger(string categoryName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you still want this change, it is fine to leave in. Otherwise if you think Moq.Protected is sufficient, we can remove this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this as I'd like for customer to be able to mock this directly :)

@davidmrdavid davidmrdavid merged commit 23eb257 into main May 17, 2024
2 checks passed
@davidmrdavid davidmrdavid deleted the dajusto/add-unit-tests branch May 17, 2024 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants