testing-unit-cs-mocks: add example of mocking Invoke (data source) calls - #2975
Merged
Merged
Conversation
Extends the existing C# unit-testing mocks example with a demonstration of mocking an azure-native Invoke (data source) call whose result contains a nested array of complex objects: ListStorageAccountKeys, the exact scenario reported in #1057. - WebsiteStack.cs now retrieves the storage account's primary key via ListStorageAccountKeys, using the InvokeAsync + Output.Tuple(...).Apply(...) pattern (the Invoke(...) Output-returning overload can be unreliable when its arguments depend on other resources' outputs). - Testing.cs's Mocks.CallAsync shows that plain nested Dictionary/List values (matching the camelCase field names of the target type) are sufficient to mock such calls - no JsonElement/JSON hand-crafting required. - WebsiteStackTests.cs adds a test asserting on the mocked value. - README.md documents the pattern.
jkodroff
approved these changes
Aug 28, 2026
Member
|
The failures are unrelated to this change. Merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1057.
Analysis
The repo did not yet have an example covering the specific pain point in #1057: mocking a data-source
Invokecall (ListStorageAccountKeys) whose result contains a nested array of complex objects (StorageAccountKeyResponse). The existingtesting-unit-cs-mocksexample only demonstrated mockingNewResourceAsync; itsCallAsyncwas an unused pass-through.I confirmed (by running
dotnet testagainst the current Pulumi/Pulumi.AzureNative packages) that the workaround discussed in the issue thread (hand-crafting aJsonElement) is no longer necessary. The mock infrastructure serializes whateverCallAsyncreturns the same way it serializes resource inputs/outputs, so a plain nestedDictionary<string, object>/List<object>— with keys matching the camelCase field names of the target type (creationTime,keyName,permissions,value) — is sufficient.Separately, while building this example I found that Pulumi.AzureNative's
Invoke(...)(Output-returning) overload can hang indefinitely under the mock test harness when its arguments depend on other resources' outputs (exactly theresourceGroupName/accountNamepattern from the issue). Wrapping the equivalentInvokeAsync(...)call inOutput.Tuple(...).Apply(...)— the same pattern the original reporter used — works reliably and is what this example now uses and documents.Changes
testing-unit-cs-mocks/WebsiteStack.cs: retrieves the storage account's primary key viaListStorageAccountKeys, using theInvokeAsync+Output.Tuple(...).Apply(...)pattern, with a comment explaining why.testing-unit-cs-mocks/Testing.cs:Mocks.CallAsyncnow mocks theazure-native:storage:listStorageAccountKeystoken, showing the plain-Dictionary/Listapproach.testing-unit-cs-mocks/WebsiteStackTests.cs: adds a test asserting on the mocked value.testing-unit-cs-mocks/README.md: documents the pattern and answers the "how do I formatStorageAccountKeyResponse" question directly.Verification
Installed the .NET 8 SDK locally and ran
dotnet testintesting-unit-cs-mocks:(previously 5 tests; added 1)
Created with Eon