|
| 1 | +--- |
| 2 | +title: ActionMock class |
| 3 | +description: Creates a mock instance for an action in a Standard logic app workflow for unit testing. |
| 4 | +services: logic-apps |
| 5 | +ms.suite: integration |
| 6 | +author: wsilveiranz |
| 7 | +ms.reviewer: estfan, azla |
| 8 | +ms.topic: reference |
| 9 | +ms.date: 06/10/2025 |
| 10 | +--- |
| 11 | + |
| 12 | +# ActionMock class |
| 13 | + |
| 14 | +**Namespace**: Microsoft.Azure.Workflows.UnitTesting.Definitions |
| 15 | + |
| 16 | +This class creates a mock instance for an action in a Standard logic app workflow. The **`*ActionMock`** class provides multiple ways to create mock actions for testing Standard workflows with static outputs, error conditions, or dynamic behavior based on execution context. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```C# |
| 21 | +// Simple mock action with success status |
| 22 | +var successAction = new ActionMock(TestWorkflowStatus.Succeeded, "SendEmail"); |
| 23 | + |
| 24 | +// A mock action with specific outputs |
| 25 | +var outputAction = new ActionMock( |
| 26 | + TestWorkflowStatus.Succeeded, |
| 27 | + "HttpRequest", |
| 28 | + new MockOutput { |
| 29 | + StatusCode = 200, |
| 30 | + Headers = JToken.Parse(@"{""Content-Type"": ""application/json""}"), |
| 31 | + Body = JToken.Parse(@"{""result"": ""success"", ""id"": 12345}") |
| 32 | + }); |
| 33 | + |
| 34 | +// Failed action with error information |
| 35 | +var failedAction = new ActionMock( |
| 36 | + TestWorkflowStatus.Failed, |
| 37 | + "DatabaseWrite", |
| 38 | + new TestErrorInfo( |
| 39 | + ErrorResponseCode.BadRequest, |
| 40 | + "Database connection failed" |
| 41 | + )); |
| 42 | + |
| 43 | +// Dynamic action that changes behavior based on execution context |
| 44 | +var dynamicAction = new ActionMock( |
| 45 | + (context) => { |
| 46 | + var inputs = context.ActionContext.ActionInputs; |
| 47 | + var amount = (int)inputs["amount"]; |
| 48 | + |
| 49 | + if (amount > 1000) { |
| 50 | + return new ActionMock(TestWorkflowStatus.Failed, "PaymentProcessing", |
| 51 | + new TestErrorInfo(ErrorResponseCode.BadRequest, "Amount exceeds limit")); |
| 52 | + } |
| 53 | + |
| 54 | + return new ActionMock(TestWorkflowStatus.Succeeded, "PaymentProcessing", |
| 55 | + new MockOutput { Body = JToken.Parse(@"{""transactionId"": ""ABC123""}") }); |
| 56 | + }, |
| 57 | + "DynamicPaymentAction"); |
| 58 | +``` |
| 59 | + |
| 60 | +## Constructors |
| 61 | + |
| 62 | +### Constructor with static outputs |
| 63 | + |
| 64 | +Creates a mock instance for **`ActionMock`** with static outputs. |
| 65 | + |
| 66 | +```C# |
| 67 | +public ActionMock(TestWorkflowStatus status, string name = null, MockOutput outputs = null) |
| 68 | +``` |
| 69 | + |
| 70 | +| Name | Description | Type | Required | |
| 71 | +|---|---|---|---| |
| 72 | +| status | The mock action result status. | [TestWorkflowStatus](test-workflow-status-enum-definition.md) | Yes | |
| 73 | +| name | The mock action name. | string | No | |
| 74 | +| outputs| The mock static outputs. | MockOutput | No | |
| 75 | + |
| 76 | +```C# |
| 77 | +// Example: Create a mock action with successful status and static outputs |
| 78 | +var outputs = new MockOutput { |
| 79 | + Body = JToken.Parse(@"{""result"": ""Operation completed""}"), |
| 80 | + StatusCode = 200 |
| 81 | +}; |
| 82 | +var actionMock = new ActionMock(TestWorkflowStatus.Succeeded, "ProcessData", outputs); |
| 83 | +``` |
| 84 | + |
| 85 | +### Constructor with error info |
| 86 | + |
| 87 | +Creates a mock instance for **`ActionMock`** with static error info. |
| 88 | + |
| 89 | +```C# |
| 90 | +public ActionMock(TestWorkflowStatus status, string name = null, TestErrorInfo error = null) |
| 91 | +``` |
| 92 | + |
| 93 | +|Name|Description|Type|Required| |
| 94 | +|---|---|---|---| |
| 95 | +|status|The mock action result status.|TestWorkflowStatus|Yes| |
| 96 | +|name|The mock action name.|string|No| |
| 97 | +|error|The mock action error info.|TestErrorInfo|No| |
| 98 | + |
| 99 | +```C# |
| 100 | +// Example: Create an action mock with failed status and error information |
| 101 | +var errorInfo = new TestErrorInfo( |
| 102 | + ErrorResponseCode.InternalServerError, |
| 103 | + "Service temporarily unavailable" |
| 104 | +); |
| 105 | +var actionMock = new ActionMock(TestWorkflowStatus.Failed, "ExternalAPICall", errorInfo); |
| 106 | +``` |
| 107 | + |
| 108 | +### Constructor with callback function |
| 109 | + |
| 110 | +Creates a mock instance for **`ActionMock`** with a callback function for dynamic outputs. |
| 111 | + |
| 112 | +```C# |
| 113 | +public ActionMock(Func<TestExecutionContext, ActionMock> onGetActionMock, string name = null) |
| 114 | +``` |
| 115 | + |
| 116 | +|Name|Description|Type|Required| |
| 117 | +|---|---|---|---| |
| 118 | +|onGetActionMock|The callback function to get the mock action|Func<[TestExecutionContext](test-execution-context-class-definition.md), [ActionMock](action-mock-class-definition.md)>|Yes| |
| 119 | +|name|The mock action name|string|No| |
| 120 | + |
| 121 | +```C# |
| 122 | +// Example: Create a mock action with dynamic outputs based on execution context |
| 123 | +var actionMock = new ActionMock( |
| 124 | + (context) => { |
| 125 | + var actionName = context.ActionContext.ActionName; |
| 126 | + var inputs = context.ActionContext.ActionInputs; |
| 127 | + // Determine outputs dynamically based on context |
| 128 | + if (actionName == "ValidateUser" && inputs["userId"]?.Value<int>() > 0) { |
| 129 | + return new ActionMock( |
| 130 | + TestWorkflowStatus.Succeeded, |
| 131 | + "ValidateUser", |
| 132 | + new MockOutput { Body = JToken.Parse(@"{""isValid"": true}") } |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + return new ActionMock(TestWorkflowStatus.Failed, "ValidateUser"); |
| 137 | + }, |
| 138 | + "ConditionalValidation"); |
| 139 | +``` |
| 140 | + |
| 141 | +### JSON constructor |
| 142 | + |
| 143 | +Creates a mock instance for **`ActionMock`** from JSON. |
| 144 | + |
| 145 | +```C# |
| 146 | +internal ActionMock(TestWorkflowStatus status, string name = null, JToken outputs = null, TestErrorInfo error = null) |
| 147 | +``` |
| 148 | + |
| 149 | +|Name|Description|Type|Required| |
| 150 | +|---|---|---|---| |
| 151 | +|status|The mock action result status.|[TestWorkflowStatus](test-workflow-status-enum-definition.md)|Yes| |
| 152 | +|name|The mock action name.|string|No| |
| 153 | +|outputs|The mock outputs.|MockOutput|No| |
| 154 | +|error|The mock error.|[TestErrorInfo](test-error-info-class-definition.md)|No| |
| 155 | + |
| 156 | +```C# |
| 157 | +// Example: Create a mock action from JSON |
| 158 | +var actionFromJson = JsonConvert.DeserializeObject<ActionMock>(File.ReadAllText(mockDataPath)); |
| 159 | +``` |
| 160 | + |
| 161 | +## Properties |
| 162 | + |
| 163 | +This class inherits the following properties from the **`OperationMock`** base class. |
| 164 | + |
| 165 | +|Name|Description|Type|Required| |
| 166 | +|---|---|---|---| |
| 167 | +|Name|Gets or sets the name for the mock operation.|string|No| |
| 168 | +|Status|Gets or sets the operation status.|[TestWorkflowStatus](test-workflow-status-enum-definition.md)|No| |
| 169 | +|Outputs|Gets or sets a value that represents static output in JSON format.|JToken|No| |
| 170 | +|Error|Gets or sets the operation error.|[TestErrorInfo](test-error-info-class-definition.md)|No| |
| 171 | + |
| 172 | +## Related content |
| 173 | + |
| 174 | +- [TestActionExecutionContext](test-action-execution-context-class-definition.md) |
| 175 | +- [TestErrorInfo](test-error-info-class-definition.md) |
| 176 | +- [TestErrorResponseAdditionalInfo](test-error-response-additional-info-class-definition.md) |
| 177 | +- [TestExecutionContext](test-execution-context-class-definition.md) |
| 178 | +- [TestIterationItem](test-iteration-item-class-definition.md) |
| 179 | +- [TestWorkflowOutputParameter](test-workflow-output-parameter-class-definition.md) |
| 180 | +- [TestWorkflowRun](test-workflow-run-class-definition.md) |
| 181 | +- [TestWorkflowRunActionRepetitionResult](test-workflow-run-action-repetition-result-class-definition.md) |
| 182 | +- [TestWorkflowRunActionResult](test-workflow-run-action-result-class-definition.md) |
| 183 | +- [TestWorkflowRunTriggerResult](test-workflow-run-trigger-result-class-definition.md) |
| 184 | +- [TestWorkflowStatus](test-workflow-status-enum-definition.md) |
| 185 | +- [TriggerMock](trigger-mock-class-definition.md) |
| 186 | +- [UnitTestExecutor](unit-test-executor-class-definition.md) |
0 commit comments